简体   繁体   English

正则表达式:搜索以 @ 或 @ 开头的单词

[英]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,}

Regex demo正则表达式演示

Another option:另外的选择:

(?<!\S)@\.?[\w.-]{2,}

Regex demo正则表达式演示

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.

相关问题 正则表达式仅搜索单词 - Regex only searching for words Python搜索两个词正则表达式 - Python searching for two words regex 在python中使用正则表达式搜索三个不同的词 - searching three different words using regex in python 正则表达式灾难性的回溯; 提取单词以大写字母开头,然后是特定单词 - regex catastrophic backtracking ; extracting words starts with capital before the specific word Pandas 正则表达式:将名称与以单词或字符串开头并以某些单词结尾的字符串分开 - Pandas Regex: Separate name from string that starts with word or start of string, and ends in certain words 正则表达式以一行中的CAPITAL词开始和结束,在CAPITAL单行词中的多行 - Regex starts and ends with CAPITAL word in a line, several lines amid CAPITAL single-line words Python 正则表达式如何找到以给定单词开头并以两个单词之一结尾的 substring - Python regex how to find a substring that starts with a given word and ends with either of two words 一个正则表达式模式,匹配所有以 s 开头的单词开始并在以 s 开头的单词之前停止的单词 - A regex pattern that matches all words starting from a word with an s and stopping before a word that starts with an s 在python中搜索“开头为”的标题? - Searching for a title thats 'starts with' in python? 在python中搜索关键字 - Searching for key words in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM