Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我必须在字符串中检查模式(模式 C)的正匹配和模式 A 和模式 B 的负匹配。如何在单个正则表达式中做到这一点?
如果存在模式 C,则字符串中不应存在模式 A 和模式 B。
请帮助我提供代码片段。以下代码不适用于 5.8.5 和 5.10.1
open(FH, "file"); while(<FH>){ print if(/(?!PATTERN A)(?!PATTERN B)(?=PATTERN C)/); } close FH;
这完全取决于您如何编写模式。
模式应始终以.*?or开头.*
.*?
.*
所以,你可以写
(?!.*?PATTERN A)(?!.*?PATTERN B)(?=.*?PATTERN C)
除非环境需要,否则不需要花哨的正则表达式。所有你需要的是
print if /PATTERN A/ and not (/PATTERN B/ or /PATTERN C/);