How do i search and extract commented lines from a file using perl?

480 views Asked by At

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.

1

There are 1 answers

2
Federico Piazza On

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:

\/\*(.*?)\*\/