简体   繁体   中英

Perl regexp match across multiple lines

If I have the following data:

<br/>
    help can be found...

So I've got this with respects to the actual data:

<br/>\n\s\s\s\shelp can be found

I can't figure out why, but Perl is not finding these matches. I'm using the following code:

my $filename = $ARGV[0];

open(INFILE,  "<",  $filename);

while (<INFILE>){
    if (/(\<br\/\>.*\s{4}[A-Z])/msi){
        print $1."\n";
 }
}

to test if Perl returns the parts in my text document that match this regexp, but it not finding them. I can't see what is wrong with my regexp. Any help would be much appreciated. I'm trying to get Perl to match across the newline character but not working.

<INFILE> in a while loop loads each line into $_ individually. So to match across lines you need to set $/ to undef. You also then need to move the while loop to the regex and use the global flag to set multiple matches.

my $filename = $ARGV[0];

$/ =undef;

open(INFILE,  "<",  $filename);

my $file = <INFILE>;

while ($file =~ /(\<br\/\>.*\s{4}[A-Z])/msig){
    print $1, "\n";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM