[英]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 ) 我有此代码行来获得具有此模式的整行,但我只对ABC字符串感兴趣(可能是其他任何字符串)
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: 如果您不想使用捕获组,则可以使用前瞻和\\K
锚:
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. \\K
基本上会丢弃之前已匹配的所有内容-但仍必须匹配。
Capture the bit that you want. 捕获所需的位。 It'll be in $1
. 它将在$1
。 (Oh, and you don't need the /g
.) (哦,您不需要/g
。)
print $1 if $line =~ /Toggle\('[A-Z]*'\)" class="Failure">(.*)<\/a>/;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.