python - 从字符串中获取 Twitter 用户名的简单正则表达式

标签 python regex

给定一个简单的字符串,如 "@dasweo where you at?" 我想编写一个正则表达式来提取 "dasweo"

我目前拥有的是:

print re.findall(r"@\w{*}", "@dasweo where you at?")

虽然这不起作用。谁能帮我解决这个问题?

最佳答案

去掉 {..} 大括号,它们不与 * 一起使用:

>>> re.findall(r"@\w*", "@dasweo where you at?")
['@dasweo']

仅使用具有固定数字的 {..} 量词:

\w{3}

例如,正好匹配 3 个字母。

关于python - 从字符串中获取 Twitter 用户名的简单正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18190930/

相关文章:

java - 将用于跳过缺失信息的正则表达式从 python 转换为 java-7

正则表达式逻辑比较简化

regex - 使用正则表达式验证密码

javascript - 正则表达式删除第一个字母,如果它是 k 和最后一个下划线之后的所有内容(包括下划线)

javascript - JS跨浏览器将 '$'字符替换为double '$$'字符的解决方案

python - 连接两个Numpy图像数组时出现问题。引发错误 “only integer scalar arrays can be converted to a scalar index”

python - Rhino.Python 和 .NET 框架

python - 从某个字符开始按空格分割Python字符串

regex - 确定行是否包含 1-4 个特定字符的正则表达式

python - 简化许多 if 语句