源串
<html name="abc:///Testers/something.txt" in="abc/Testers/something.txt" loci="123" sap="abcdefgh="/><html name="abc:///needed.txt" src="abc/needed.txt" location="123" sap="rtyghu"/><html name="abc:///Testers/Testers3/Another.txt" in="abc/Testers/Testers3/Another.txt" loci="123" sap="jhkiopjhg"/><html name="abc:///onemore.txt" src="abc/onemore.txt" location="123" sap="dfrtyu"/>如何匹配从<html name=" not followed by (needed) or (onemore) and ending with />开始的部分
所以在这个字符串中应该有两个匹配项,分别是
<html name="abc:///Testers/something.txt" in="abc/Testers/something.txt" loci="123" sap="abcdefgh="/>
<html name="abc:///Testers/Testers3/Another.txt" in="abc/Testers/Testers3/Another.txt" loci="123" sap="jhkiopjhg"/>我试过了- <html name=(?!(needed|onemore)).*?"\/>
它不工作,因为我混淆了非贪婪和消极的前瞻东西。
发布于 2017-07-25 21:05:42
发布于 2017-07-25 21:13:21
下面是您的正则表达式<html name=(?!(needed|onemore)).*?"\/>的细目
<html name=(?!(needed|onemore)).*?"\/>
1) Literal match: <html name=
2) Not followed by: "needed" or "onemore"
3) Lazy grab all: .*?
Until Literal match: "/>您需要做的是使用另一个像这样的<html name=(?:(?!(needed|onemore)).)*?"\/>分组来检查每个字符抓取是否需要或多个字符。这将检查在每个字符抓取中是否紧跟着"needed“或"onemore”。(我还建议使用[^>]而不是.,这样您就不需要惰性量词了。)
但是,我建议您在过滤<html name=([^>no]|n(?!eeded)|o(?!nemore))*>时使用类似这样的东西。更容易适应,并且对正则表达式引擎的工作更少。
https://stackoverflow.com/questions/45303746
复制相似问题