-2

I am new to Perl programming. Need your help in searching and extracting only commented lines from a file. Below is an example

{
    This is line 1
    /*This is line 2
    This is line 3
    This is line 4*/
    This is line 5
}

I just want to search and extract only commented lines from above file.

lakshayg
  • 2,053
  • 2
  • 20
  • 34
sravan
  • 1
  • 2

1 Answers1

1

You can use a regex like this:

\/\*([\s\S]*?)\*\/

Working demo

Code

my $str = 'you string';
if ( $str =~ /\/\*([\s\S]*?)\*\// ) {
    print "Comment: $1";
}

As Borodin pointed in his comment, you can use dot with s flag (single line) instead of [\s\S], so you could change your regex to:

\/\*(.*?)\*\/
Community
  • 1
  • 1
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123