Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 具有负前瞻性和非贪婪性的正则表达式匹配_Regex_Pcre - Fatal编程技术网

Regex 具有负前瞻性和非贪婪性的正则表达式匹配

Regex 具有负前瞻性和非贪婪性的正则表达式匹配,regex,pcre,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"

源字符串

<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\s+name="(?![^"]*(?:needed|onemore))[^>]*>
]*>

除了对应该放弃遍历的位置进行限制外,还需要使用重复量词:

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

这是您的regex

1) 文字匹配:
。这将检查“需要”或“更多”不是每个角色抓取的下一个。(我还建议使用
[^>]
而不是
,这样就不需要惰性量词。)


但是,我建议您在过滤时使用类似的方法
no]|n(?eeded)|o(?nemore))*>
。regex引擎更容易适应,工作量更少。

以下是您的regex

1) 文字匹配:
。这将检查“需要”或“更多”不是每个角色抓取的下一个。(我还建议使用
[^>]
而不是
,这样就不需要惰性量词。)


但是,我建议您在过滤时使用类似的方法
no]|n(?eeded)|o(?nemore))*>
。regex引擎更容易适应,工作量更少。

OP是否只想在
name
属性或标记中的任何位置检查黑名单单词?如果是后者,你的消极预测也会失败。我也不确定。但如果是这样的话,我会更新答案,将其包含在
]*(?:needed | onemore))[^>]*>
别忘了以/>结尾的
(:我向上投票给你是因为,信不信由你,我从来没有这样限制过向前看+1OP不是验证主题字符串而是匹配。斜杠的存在与给定的解决方案并不冲突。@MateusA.OP是否只想在
name
属性或标记中的任何地方检查黑名单中的单词?如果是后者,你可以r反向前瞻也会失败。我也不确定。但如果是这种情况,我会更新答案,将其包含在
]*(?:needed | onemore))[^>]*>
别忘了
以/>
结尾的部分(:我向上投票给你是因为,信不信由你,我从来没有这样限制过向前看,+1OP不是验证主题字符串而是匹配。斜杠的存在与给定的解决方案没有冲突。@MateusA。
<html name=(?!(needed|onemore)).*?"\/>
1) Literal match: <html name=
2) Not followed by: "needed" or "onemore"
3) Lazy grab all: .*?
  Until Literal match: "/>