[英]Regex should fail if pattern is followed by another pattern
I need to detect @username
mentions within a message, but NOT if it is in the form of @username[user_id]
.我需要检测消息中提到的
@username
,但如果它是@username[user_id]
的形式则不需要。 I have a regex that can match the @username
part, but am struggling to negate the match if it is followed by \[\d\]
.我有一个可以匹配
@username
部分的正则表达式,但如果它后面跟着\[\d\]
,我很难否定匹配。
import re
username_regex = re.compile(r'@([\w.@-]+[\w])')
usernames = username_regex.findall("Hello @kevin") # correctly finds kevin
usernames = username_regex.findall("Hello @kevin.") # correctly finds kevin
usernames = username_regex.findall("Hello @kevin[1].") # shouldn't find kevin but does
The regex allows for usernames that contain @
, .
正则表达式允许包含
@
, 的用户名.
and -
, but need to end with a \w
character ( [a-zA-Z0-9_]
).和
-
,但需要以\w
字符( [a-zA-Z0-9_]
)结尾。 How can I extend the regex so that it fails if the username is followed by the userid in the [1]
form?如果用户名后跟
[1]
表单中的用户 ID,我如何扩展正则表达式以使其失败?
I tried @([\w.@-]+[\w])(?!\[\d+\])
but then it matches kevi
我试过
@([\w.@-]+[\w])(?!\[\d+\])
但它匹配kevi
I'm using Python 3.10.我正在使用 Python 3.10。
You can "emulate" possessive matching with您可以“模拟”所有格匹配
@(?=([\w.@-]*\w))\1(?!\[\d+\])
See the regex demo .请参阅正则表达式演示。
Details :详情:
@
- a @
char @
- 一个@
字符(?=([\w.@-]*\w))
- a positive lookahead that matches and captures into Group 1 zero or more word, .
(?=([\w.@-]*\w))
- 匹配并捕获到第 1 组零个或多个单词的正向前瞻, .
, @
and -
chars, as many as possible, and then a word char immediately to the right of the current position (the text is not consumed, the regex engine index stays at the same location) @
和-
字符,尽可能多,然后在当前 position 右侧紧接一个单词 char(文本不被消耗,正则表达式引擎索引保持在同一位置)\1
- the text matched and captured in Group 1 (this consumes the text captured with the lookahead pattern, mind that backreferences are atomic by nature) \1
- 在第 1 组中匹配和捕获的文本(这会消耗使用前瞻模式捕获的文本,请注意反向引用本质上是原子的)(?!\[\d+\])
- a negative lookahead that fails the match if there is [
+ one or more digits + ]
immediately to the right of the current location. (?!\[\d+\])
- 如果当前位置右侧有[
+ 一位或多位数字 + ]
则匹配失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.