2024-04-19 17:48:25 发布
网友
我想匹配一个字符串,其中有“subscribe”和没有“did not”或“unsub.*”。你知道吗
例如
“请订阅我”将匹配
但“我没有订阅这封邮件”或“请取消订阅我”将无法匹配。你知道吗
我所拥有的是
".*subscribe(?!.*did\\s+not)(?!.*unsub.*)"
这显然行不通。你知道吗
所以再说一遍,我想要的表达是(和!B还有!(三)
任何帮助都将不胜感激。你知道吗
谢谢你
埃里克
正则表达式:
^(?!.*unsub)(?!.*did not).*subscribe
Python:
re.match(r"^(?!.*unsub)(?!.*did not).*subscribe", str)
您可以使用正向和反向lookahead来实现这一点,但是更好的方法是使用一个regexp作为搜索词,另一个用于stopwords。你知道吗
if re.search(r"\bsubscribe", text) and not re.search(r"did\s+not|\bunsub", text): unsubscribe(sender)
Lookaheads只有在需要在特定位置包含/排除文本时才值得使用。你知道吗
还要注意,\b(单词边界)将防止“subscribe”与单词“unsubscribe”匹配。你知道吗
\b
lookaheads应该位于正则表达式的开头:
re.match(r"(?!.*did\s+not)(?!.*unsub).*subscribe", text)
正则表达式:
Python:
您可以使用正向和反向lookahead来实现这一点,但是更好的方法是使用一个regexp作为搜索词,另一个用于stopwords。你知道吗
Lookaheads只有在需要在特定位置包含/排除文本时才值得使用。你知道吗
还要注意,
\b
(单词边界)将防止“subscribe”与单词“unsubscribe”匹配。你知道吗lookaheads应该位于正则表达式的开头:
相关问题 更多 >
编程相关推荐