基本上我想在未注释的行上找到foobar:
// foobar # do not match me
foobar # match me
otherfoobar # do not match me
为了匹配注释行,我使用以下命令: ^(\s*//\s*)\bfoobar\b.*$
如何匹配未注释的行?
笔记:
为避免不必要的工作,请将答案限制为“ ^ ... $”。这必须完全在发送给perl进行匹配的字符串中完成。
而且foobar必须与\ bfoobar \ b中的整个匹配
最后,\ bfoobar \ b不应以空格或注释签名开头。
我认为否定的问题不应过于复杂。
例如说我要拒绝这些行:
"^(\s*//\s*).*(\bfoobar\b).*$"
IE。首先'//'
在空白处的任何地方都具有注释签名,然后是包含'foobar'
整个单词的所有内容(为清楚起见,包括了方括号)。
否定这个,我们要做:
"^(?!\s*//\s*).*(\bfoobar\b).*$"
我们只是替换(\s*//\s*)
为(?!\s*//\s*)
这无视任何'//'
在开头的空白处带有注释签名的内容,其后是我们的模式。
结果:
// foobar will not be matched
// whatever foobar will not be matched
foobar will be matched
whatever foobar will be matched
whateverfoobar will not be matched
注意:您可以通过更改.*
之前(\bfoobar...
和设置来进一步进行微调。
我可以 "^(?!\s*//\s*)\s*(\bfoobar\b).*$"
foobar will be matched
whatever foobar will **NOT** be matched
警告:如果您'foobar'
用任意字符串替换,例如,'${foobar}'
然后尝试上述操作,它将失败。原因是因为您的普通“ foobar”仅由文字字符组成,现在(\bfoobar\b)
不匹配。在这种情况下,\b
必须将其卸下。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句