Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的重复正则表达式模式_Python_Regex - Fatal编程技术网

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,而是搜索