简体   繁体   中英

Perl get string between 2 patterns

I have a log file that has this pattern several times:

Toggle('AFDACAAAAAIAAAA')" class="Failure">ABC</a> 

I have this code line to get the entire line that has this pattern but I'm only interested in the ABC string ( that may be any other string )

print "$line" if $line =~ /Toggle\('[A-Z]*'\)" class="Failure">.*<\/a>/g;

Can I do that with regex ?

If you don't want to use a capture group you can use a look ahead and the \\K anchor:

print $& if $line =~ /Toggle\('[A-Z]*'\)" class="Failure">\K.*(?=<\/a>)/;

\\K will basically throw away everything that has been matched before it - but it still has to match.

Capture the bit that you want. It'll be in $1 . (Oh, and you don't need the /g .)

print $1 if $line =~ /Toggle\('[A-Z]*'\)" class="Failure">(.*)<\/a>/;

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