regex - 避免使用正则表达式的 Twitter 配置文件名称 (@Profile)

标签 regex

我正在尝试分析推文,但希望使用正则表达式避免配置文件用户名后跟 @ (@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/

相关文章:

javascript - 在正则表达式中允许 "/"正斜杠

python - 在 python 中,如何在我选择的子字符串之前只搜索一个词

php - 在 PHP 中合并正则表达式

javascript - JQuery 正则表达式电子邮件验证阻止表单提交

regex - 如何在正则表达式中匹配包含特殊字符的整个单词

正则表达式,提取字符串末尾的单词和数字

regex - 在 shell 中使用转义字符

regex - 仅在外部嵌套代码块的开头和结尾插入代码

php - 正则表达式是否足够或我需要更多检查?

c++ - QRegularExpression - 向后查找与捕获长度的匹配