我想提取中国微博的用户名。所以我使用这个代码:
def atExtractor(sentence):
return re.findall("@.*\\s", sentence, re.I)然后我摘录这句话:
atExtractor(u"@中国联通网上营业厅 @北京地铁 北京地铁10号线,从惠新西街南口到海淀黄庄")它得到:
[u'@中国联通网上营业厅 @北京地铁 ']为什么裁判只能得到一次而不是两次?当我想提取hashtag时,也会发生同样的问题:
def activityExtractor(sentence):
return re.findall("#.*#", sentence, re.I)
activityExtractor(u"#中国联通网上营业厅# #北京地铁# 北京地铁10号线")它得到:
[u'#中国联通网上营业厅# #北京地铁# ']发布于 2015-08-31 15:49:40
因为你的模式很贪婪。
re.findall("@.*?(?=\\s)", sentence, re.I)或
re.findall(r"@\S*", sentence, re.I)\S*应该匹配零或多个非空格字符。
https://stackoverflow.com/questions/32315126
复制相似问题