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 使用re.match时具有lookbehind的正则表达式不起作用_Python_Regex_String_Lookbehind - Fatal编程技术网

Python 使用re.match时具有lookbehind的正则表达式不起作用

Python 使用re.match时具有lookbehind的正则表达式不起作用,python,regex,string,lookbehind,Python,Regex,String,Lookbehind,下面是python代码: import re line="http://google.com" procLine = re.match(r'(?<=http).*', line) if procLine.group() == "": print(line + ": did not match regex") else: print(procLine.group()) 重新导入 行=”http://google.com" procLine=re.match(r’(?如果您

下面是python代码:

import re

line="http://google.com"
procLine = re.match(r'(?<=http).*', line)
if procLine.group() == "":
    print(line + ": did not match regex")
else:
    print(procLine.group())
重新导入
行=”http://google.com"

procLine=re.match(r’(?如果您将lookback转换为非捕获组,那么这应该可以工作:

In [7]: re.match(r'(?:http://)(.*)', line)
Out[7]: <_sre.SRE_Match object; span=(0, 17), match='http://google.com'>

In [8]: _.group(1)
Out[8]: 'google.com'

您可能希望使用
search
,请选中:“注意,以正向查找断言开头的模式在被搜索字符串的开头不匹配;您很可能希望使用search()函数而不是match()函数”谢谢,这确实有效,我将使用它作为解决方法,但我暂时不把它标记为正确,看看是否有人知道为什么lookbehinds似乎让我失望。@LostCrotchet因为
match
在字符串开头应用正则表达式。字符串开头的lookbehind永远不会起作用。@Rawing这是我的怀疑,但我不想在不确定的情况下写这些。请告诉我是否可以添加这些内容,或者您可以创建一个答案。@cᴏʟᴅsᴘᴇᴇᴅ 继续。我认为,每当有人发表评论而不是回答时,他们都在暗中允许你使用它:)@LostCrotchet ping你看这篇文章。
In [10]: re.search(r'(?<=http://).*', line)
Out[10]: <_sre.SRE_Match object; span=(7, 17), match='google.com'>

In [11]: _.group()
Out[11]: 'google.com'