Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.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,我有这一段: “这是下一段包含主控程序代码的内容 预算和其他主预算。此外,它只包含主预算 预算条款。但是只有这个预算应该匹配。但是这个预算 不会被匹配” 在这里,我试图只匹配单词“budget”的第一个匹配项,如果前面有“master”或“other master”,则跳过所有出现的budget。我使用的是“消极的回头看”,并提出了一段在网站上运行良好的代码: p=re.compile(r')(?您得到的错误是因为Python中的lookbehinds应该是固定长度的,而(?)长度不是固定的 自

我有这一段:

“这是下一段包含主控程序代码的内容 预算和其他主预算。此外,它只包含主预算 预算条款。但是只有这个预算应该匹配。但是这个预算 不会被匹配”

在这里,我试图只匹配单词“budget”的第一个匹配项,如果前面有“master”或“other master”,则跳过所有出现的budget。我使用的是“消极的回头看”,并提出了一段在网站上运行良好的代码:


p=re.compile(r')(?您得到的错误是因为Python中的lookbehinds应该是固定长度的,而
(?)长度不是固定的

(?<!master|master other)
(?
相当于

(?<!master)(?<!master other)
(?
您可以将正则表达式更改为:

((?<!master)(?<!master other)\s\bbudget\b)

(?谢谢Maroun,我会试试。不需要使用外圆括号。该值可以通过
match.group(0)
match.group()
轻松访问。这是一个已知的值。还有:(
((?<!master)(?<!master other)\s\bbudget\b)