简体   繁体   English

如果模式后面跟着另一个模式,正则表达式应该会失败

[英]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.

相关问题 Python正则表达式匹配所有出现的十进制模式,后跟另一个模式 - Python Regex match all occurrences of decimal pattern followed by another pattern 如果后面没有模式,则正则表达式匹配 - regex match if not followed by pattern 匹配整个单词或单词后跟另一个的正则表达式模式 - regex pattern to match whole word or word followed by another 这应该是什么正则表达式模式? - What should be the regex pattern for this? 正则表达式:匹配一种模式并排除另一种模式 - Regex: match one pattern and exclude another pattern python 正则表达式将文本从特定模式打印到另一个模式,但条件是特定字符串应该存在于两者之间 - python regex to print text from a specific pattern to another pattern, but in condition that a specific string should exist in between 正则表达式:将一种模式替换为另一种 - Regex: Replace one pattern with another 正则表达式获取所有出现的模式,后跟逗号分隔字符串中的值 - Regex to get all occurrences of a pattern followed by a value in a comma separate string Python - 正则表达式找到重复的模式,然后是可变长度的字符 - Python - Regex findall repeated pattern followed by variable length of chars 我应该匹配某个模式重复项和一个可选模式的Python正则表达式在做什么? - What am I doing wrong with this Python regex that is supposed to match repeats of a pattern, followed by an optional pattern?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM