提取特定字符后的文本 [英] Extract text after specific character
问题描述
我需要提取@
我该怎么做?我正在尝试:
text="你好@bob !"user=text[text.find("@")+1:]打印用户
输出:
鲍勃!
但正确的输出应该是:
鲍勃
一个有趣的正则表达式解决方案:
<预><代码>>>>进口重新>>>re.findall(r'@(\w+)', '@Hello there @bob @!')['你好','鲍勃']>>>re.findall(r'@(\w+)', '你好,鲍勃!')[]>>>(re.findall(r'@(\w+)', 'Hello there @bob !') 或 None,)[0]'鲍勃'>>>打印 (re.findall(r'@(\w+)', 'Hello there bob !') 或 None,)[0]没有任何上面的正则表达式将选取一个或多个字母数字字符的模式,直到找到一个非字母数字字符.
如果您想捕获更广泛的子字符串,这里有一个匹配一个或多个非空白字符的正则表达式解决方案:
<预><代码>>>>re.findall(r'@(\S+?)', '@Hello there @bob @!')['你好','鲍勃','!']请注意,当上面的正则表达式遇到像 @xyz@abc
这样的字符串时,它会在一个结果中捕获 xyz@abc
而不是 xyz
和 abc
分开.为了解决这个问题,你可以使用否定的 \s
字符类,同时也否定 @
字符:
这里有一个正则表达式解决方案,用于匹配一个或多个字母字符,以防万一您不想要任何数字或其他任何东西:
<预><代码>>>>re.findall(r'@([A-Za-z]+)', '@你好@bobv2.0 @!')['你好', 'bobv']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
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@abc
it will capture xyz@abc
in one result instead of xyz
and abc
separately. To fix that, you can use the negated \s
character class while also negating @
characters:
>>> 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']
这篇关于提取特定字符后的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!