【发布时间】:2015-09-05 13:58:24
【问题描述】:
我想在文档中搜索以下字符串
<whoName>[substring]</whoName>
where substring !="self"
所以
<whoName>other</whoName>
会通过的。
但是
<whoName>self</whoName>
会失败
【问题讨论】:
我想在文档中搜索以下字符串
<whoName>[substring]</whoName>
where substring !="self"
所以
<whoName>other</whoName>
会通过的。
但是
<whoName>self</whoName>
会失败
【问题讨论】:
您可以使用这个基于负前瞻的正则表达式:
<(whoName)>(?!self\s*<).*?<\/\1>
正则表达式分解:
<(whoName)> # Match <whoName> and capture it ($1)
(?!self\s*<) # Negative lookahead that means current position is not followed by literal
# self followed by 0 or more spaces and <
.*? # Match 0 or more characters (non-greedy)
<\/\1> # Match closing tag using back-reference to captured group #1
【讨论】:
使用这个正则表达式:
^<whoName>((?!self<).)*</whoName>
在notepad++和regex101中测试
【讨论】:
您可以找到解决方案here。
你可以使用类似的东西
<whoName>(?!self)[a-z0-9]*<\/whoName>
(在这里试试这个:https://regex101.com/r/vW8sP3/1)。
您可以想象相关部分是(?!self)。
【讨论】: