[英]Regex: searching for words that starts with @ or @
I want to create a regex in python that find words that start with @ or @.我想在 python 中创建一个正则表达式来查找以 @ 或 @ 开头的单词。
I have created the following regex, but the output contains one extra space in each string as you can see我创建了以下正则表达式,但输出在每个字符串中包含一个额外的空格,如您所见
regex = r'\s@\/?[\w\.\-]{2,}'
exp = 'george want@to play @.hdgska football @dddada'
re.findall(regex, exp)
Output: [' @.hdgska', ' @dddada']
However, the output that I want to have is the following但是,我想要的输出如下
Output: ['@.hdgska', '@dddada']
I would be grateful if you could help me!如果您能帮助我,我将不胜感激!
Edit: @The fourth bird, Thank you so much for your help.编辑: @第四只鸟,非常感谢您的帮助。 There is one more thing that I don't know how to deal with.
还有一件事我不知道如何处理。 In case that we have this string
如果我们有这个字符串
s = "george want@to play @.hdgska football @dddada@snhfbjskjs"
the output is输出是
['@.hdgska', '@dddada']
However, the output that I want should be one this但是,我想要的输出应该是这个
'@.hdgska'
In your pattern you are actually matching the leading \s
and after the @ there can be an optional /
with \/?
在您的模式中,您实际上匹配的是前导
\s
并且在 @ 之后可以有一个可选的/
和\/?
but it should optionally start with a dot.但它应该可选地以点开头。
You could match for example an optional dot, and then 2 or more times the allowed characters in the character class.例如,您可以匹配可选的点,然后匹配字符类中允许的字符的 2 倍或更多倍。
At the left of the @ sign, either assert a non word boundary or assert a whitespace boundary.在 @ 符号的左侧,断言非单词边界或断言空白边界。
Note that you don't have to escape the dot and the hyphen in the character class.请注意,您不必转义字符类中的点和连字符。
\B@\.?[\w.-]{2,}
Another option:另外的选择:
(?<!\S)@\.?[\w.-]{2,}
Example例子
import re
pattern = r"(?<!\S)@\.?\w+[\w.-]{2,}"
s = "george want@to play @.hdgska football @dddada"
print(re.findall(pattern, s))
Output输出
['@.hdgska', '@dddada']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.