Python 提取特定字符后的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29836812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:04:47  来源:igfitidea点击:

Extract text after specific character

pythonpython-2.7

提问by user2990084

I need to extract the word after the @

我需要提取后面的单词 @

How can I do that? What I am trying:

我怎样才能做到这一点?我在尝试什么:

text="Hello there @bob !"
user=text[text.find("@")+1:]
print user

output:

输出:

bob !

But the correct output should be:

但正确的输出应该是:

bob

采纳答案by Shashank

A regex solution for fun:

一个有趣的正则表达式解决方案:

>>> import re
>>> re.findall(r'@(\w+)', '@Hello there @bob @!')
['Hello', 'bob']
>>> re.findall(r'@(\w+)', 'Hello there bob !')
[]
>>> (re.findall(r'@(\w+)', 'Hello there @bob !') or None,)[0]
'bob'
>>> print (re.findall(r'@(\w+)', 'Hello there bob !') or None,)[0]
None

The regex above will pick up patterns of one or more alphanumeric characters following an '@' character until a non-alphanumeric character is found.

上面的正则表达式将选择一个或多个字母数字字符的模式,直到找到一个非字母数字字符。

Here's a regex solution to match one or more non-whitespace characters if you want to capture a broader range of substrings:

如果您想捕获更广泛的子字符串,这是匹配一个或多个非空白字符的正则表达式解决方案:

>>> re.findall(r'@(\S+?)', '@Hello there @bob @!')
['Hello', 'bob', '!']

Note that when the above regex encounters a string like @xyz@abcit will capture xyz@abcin one result instead of xyzand abcseparately. To fix that, you can use the negated \scharacter class while also negating @characters:

需要注意的是,当上述正则表达式遇到像绳子@xyz@abc将捕获xyz@abc的一个结果,而不是xyzabc独立。要解决这个问题,您可以使用否定\s字符类,同时也否定@字符:

>>> re.findall(r'@([^\s@]+)', '@xyz@abc some other stuff')
['xyz', 'abc']

And here's a regex solution to match one or more alphabet characters only in case you don't want any numbers or anything else:

这是一个正则表达式解决方案,仅在您不需要任何数字或其他任何内容时才匹配一个或多个字母字符:

>>> re.findall(r'@([A-Za-z]+)', '@Hello there @bobv2.0 @!')
['Hello', 'bobv']

回答by ODiogoSilva

So you want the word starting after @ up to a whitespace?

所以你想要从@ 开始的单词到一个空格?

user=text[text.find("@")+1:].split()[0]
print(user)
bob

EDIT: as @bgstech note, in cases where the string does not have a "@", make a check before:

编辑:作为@bgstech 注意,在字符串没有“@”的情况下,请先检查:

if "@" in text:
    user=text[text.find("@")+1:].split()[0]
else:
    user="something_else_appropriate"