正则表达式与负向前瞻和非贪婪相匹配

时间:2017-07-25 12:45:52

标签: regex pcre

源字符串

<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)).*?"\/>

它不起作用,因为我对非贪婪和消极的前瞻性东西感到困惑。

2 个答案:

答案 0 :(得分:4)

你需要的是使用重复量词,除了限制它应该放弃遍历的位置:

<html\s+name="(?![^"]*(?:needed|onemore))[^>]*>

Live demo

答案 1 :(得分:2)

以下是正则表达式<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)).)*?"\/>)检查每个字符抓取是否需要。这将检查每个角色抓取下次“不需要”或“再一次”。 (我还建议使用[^>]代替.,这样您就不需要延迟量词。)

但是,我建议您使用类似的内容进行过滤<html name=([^>no]|n(?!eeded)|o(?!nemore))*>。更容易适应和减少正则表达式引擎的工作。