Skip to main content
 首页 » 编程设计

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

2025年05月04日20mayingbao

给定一个简单的字符串,如 "@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 个字母。