我有这个代码:
replies = re.findall(r'@([^\\s]+)', string_content_here)
我想要做的是查找@符号之后的所有用户名。我只想在空格、逗号、感叹号和问号之前查找 @ 字符串。
示例:
@Spindel <--- Should return Spindel
@Spindel, <--- Should return Spindel
@Spindel! <--- Should return Spindel
@Spindel? <--- Should return Spindel
@gmail.com <--- I don't want this at all
最佳答案
r'@([\w]+)(?:$|[ ,!?])'
?:
的作用是使第二组“不捕获”,因此您只能在结果中获得所需的名称。
关于python - 正则表达式 - 查找 @ 之后和特定字符之前的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26482646/