我正在尝试分析推文,但希望使用正则表达式避免配置文件用户名后跟 @ (@Profile_name)!
我试过:
re.findall(r'(?!@[\w+]*)(\w+)', "I want to take everything but @this, but I cannot find a way"))
它给了我:
>>>> [['I', 'want', 'to', 'take', 'everything', 'but', 'this', 'but', 'I', 'cannot', 'find', 'a', 'way']]
我不想要“这个”:/ 我是正则表达式的新手,但我真的无法解决这个问题。 谢谢!
最佳答案
尝试 re.sub
re.sub(pattern, repl, string, count=0, flags=0)
Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl. If the pattern isn’t found, string is returned unchanged. repl can be a string or a function; if it is a string, any backslash escapes in it are processed. That is,
\n
is converted to a single newline character,\r
is converted to a carriage return, and so forth. Unknown escapes such as\j
are left alone. Backreferences, such as\6
, are replaced with the substring matched by group 6 in the pattern.
>>> re.sub(r'(@\w+)', "", "I want to take everything but @this, but I cannot find a way")
'I want to take everything but , but I cannot find a way'
关于regex - 避免使用正则表达式的 Twitter 配置文件名称 (@Profile),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48869132/