Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.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_Lookbehind - Fatal编程技术网

带条件查找的python正则表达式

带条件查找的python正则表达式,python,regex,lookbehind,Python,Regex,Lookbehind,我正在寻找以@开头并以第一次出现的\s结尾的子字符串。 字符串开头或空格后必须有@ 示例:@one bla bla bla@two@three@four#@five 结果:@one、@two、@three@four 我以以下内容结束:(?您的捕获组没有捕获您真正想要的文本: (?:(?<=^)|(?<=\s))(@[^\s]+) (?:(?>关于findall(r')(?:(?如果您不愿意使用reg expr,您可以尝试: >>> s ="@one bla bla

我正在寻找以
@
开头并以第一次出现的
\s
结尾的子字符串。 字符串开头或空格后必须有
@

示例
@one bla bla bla@two@three@four#@five

结果
@one、@two、@three@four


我以以下内容结束:
(?您的捕获组没有捕获您真正想要的文本:

(?:(?<=^)|(?<=\s))(@[^\s]+)

(?:(?>关于findall(r')(?:(?如果您不愿意使用reg expr,您可以尝试:

>>> s ="@one bla bla bla @two @three@four #@five"
>>> filter(lambda x:x.startswith('@'), s.split())
['@one', '@two', '@three@four']

这实际上应该快得多…

您如何在Python中使用此正则表达式?您不需要在第一个分支中进行查找。
^
已经是零宽度断言。值得一提的是,这种行为的原因是,如果存在捕获组,
findall
将返回它们,而不是返回整个match(即使它在没有分组的情况下返回整个比赛)。这是有记录的,但它似乎总是让人们感到惊讶。@BrenBarn:嗯,我不知道。谢谢。
>>> re.findall(r'(?:(?<=^)|(?<=\s))(@[^\s]+)', '@one bla bla bla @two @three@four #@five')
['@one', '@two', '@three@four']
>>> s ="@one bla bla bla @two @three@four #@five"
>>> filter(lambda x:x.startswith('@'), s.split())
['@one', '@two', '@three@four']