Python中的重复正则表达式模式
我有一个包含数百万转发的文件,如下所示:Python中的重复正则表达式模式,python,regex,Python,Regex,我有一个包含数百万转发的文件,如下所示: RT @Username: Text_of_the_tweet RT @ReutersAero: Further pictures from the #MH17 crash site in in Grabovo, #Ukraine #MH17 - @reuterspictures (GRAPHIC): http://t.co/4rc7Y4… RT @(\w+): 我只需要从这个字符串中提取用户名。 因为我在正则表达式方面完全是零,所以在这里有人建
RT @Username: Text_of_the_tweet
RT @ReutersAero: Further pictures from the #MH17 crash site in in Grabovo, #Ukraine #MH17 - @reuterspictures (GRAPHIC): http://t.co/4rc7Y4…
RT @(\w+):
我只需要从这个字符串中提取用户名。
因为我在正则表达式方面完全是零,所以在这里有人建议我使用
username = re.findall('@([^:]+)', retweet)
这在大多数情况下都很有效,但有时我会听到这样的句子:
RT @Username: Text_of_the_tweet
RT @ReutersAero: Further pictures from the #MH17 crash site in in Grabovo, #Ukraine #MH17 - @reuterspictures (GRAPHIC): http://t.co/4rc7Y4…
RT @(\w+):
我只需要字符串中的ReutersAero,但由于它包含另一个@and:它会弄乱正则表达式,我得到以下输出:
['ReutersAero', 'reuterspictures (GRAPHIC)']
有没有办法只在字符串中找到的第一个实例中使用正则表达式?您可以这样使用正则表达式:
RT @Username: Text_of_the_tweet
RT @ReutersAero: Further pictures from the #MH17 crash site in in Grabovo, #Ukraine #MH17 - @reuterspictures (GRAPHIC): http://t.co/4rc7Y4…
RT @(\w+):
匹配信息:
您可以使用以下python代码:
您可以使用这样的正则表达式:
RT @Username: Text_of_the_tweet
RT @ReutersAero: Further pictures from the #MH17 crash site in in Grabovo, #Ukraine #MH17 - @reuterspictures (GRAPHIC): http://t.co/4rc7Y4…
RT @(\w+):
匹配信息:
您可以使用以下python代码:
是否有一种方法可以只在字符串中找到的第一个实例中使用正则表达式
不要使用findall,而是搜索
是否有一种方法可以只在字符串中找到的第一个实例中使用正则表达式
不要使用findall,而是搜索