[英]Regexp match except on comment
I'm trying to find all occurrences of some code unless that code is preceded by a comment. 我正在尝试查找某些代码的所有出现,除非该代码前面带有注释。
Here's an example of what I want to find: 这是我要查找的示例:
$page_content .= '<meta http-equiv="refresh"
or 要么
$page_content .= 'Some other text here</p><meta http-equiv="refresh"
With or without preceding white space. 有或没有前面的空格。 Here's what I want to ignore
这就是我要忽略的
//$page_content .= '<meta http-equiv="refresh"
again with or without preceding white space. 再有或没有前面的空白。
That way I can be sure that my code base never contains this code unless it's in a comment or set up an automatic alert if it is found without getting false alerts for when its commented out (ignore multi-line comments for now). 这样,我可以确保我的代码库永远不会包含该代码,除非它在注释中,或者设置一个自动警报(如果找到它)而注释掉时不会得到错误警报(暂时忽略多行注释)。
I've tried using look behind 我试着用后面看
(?<!\/\/).*<meta http-equiv="refresh"
but I've not had much luck as this still matches every occurrence, commented or not. 但我运气不佳,因为无论发生与否,这仍然可以匹配所有情况。
One more thing: It would be great if it was in one regex rather in a loop of code so that I can search in Notepad++ or other editor that supports reg exp searches. 还有一件事:最好是在一个正则表达式中而不是在代码循环中,这样我可以在Notepad ++或其他支持reg exp搜索的编辑器中进行搜索。 (Its amazing how differently one question can be read/understood. I'd thought I'd been pretty clear but from the variety of completely valid answers its clear that I could have included a lot more detail :-)
(令人惊讶的是,一个问题可以被读/理解的方式有多么不同。我以为我已经很清楚了,但是从各种各样的完全有效的答案来看,我显然可以提供更多细节了:-)
Just remove the comment before checking for the string 在检查字符串之前只需删除注释
while ( <$fh> ) {
s|//.*||;
if ( /<meta http-equiv="refresh"/ ) {
...;
}
}
For the input specified in question : 对于有问题的指定输入:
//$page_content .= '<meta http-equiv="refresh"
This will do the thing: 这样就可以了:
use strict;
use warnings;
open my $fh, "<", "my_path\\data.txt";
while ( my $line = <$fh>) {
if ( $line =~ /^(?!\/\/).*?<meta http-equiv=\"refresh\"/){
print $line;
}
}
If you have more spaces or other indent operators, use a look-behind operator: use strict; 如果您有更多的空格或其他缩进运算符,请使用后向运算符:
use warnings;
open my $fh, "<", "c:\\users\\uidp7702\\desktop\\data.txt";
while ( my $line = <$fh>) {
if ( $line =~ /(?<!\/\/)\$page_content\s.=\s\'.*?<meta http-equiv=\"refresh\"/){
print $line;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.