Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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正则表达式错误:缺失),位置35处未终止的子模式_Python_Regex_Python 3.x - Fatal编程技术网

Python正则表达式错误:缺失),位置35处未终止的子模式

Python正则表达式错误:缺失),位置35处未终止的子模式,python,regex,python-3.x,Python,Regex,Python 3.x,我有这个正则表达式模式: (?P.*)(?。+)(?.*) 这个正则表达式应该匹配如下字符串: 你好,我的朋友(敌人)很高兴再次见到你 prefix组应捕获hello my words小组应捕获朋友|敌人 postfix组应该捕获很高兴再次见到你 此正则表达式还使用lookbehinds检查是否使用字符串中的\对(和)进行了转义。例如,由于在(和)之前存在\): 你好,我的\(朋友|敌人)很高兴再次见到你 你好,我的(朋友|敌人\)很高兴再次见到你 当我使用在线网站检查该模式时,它运行良好,但当

我有这个正则表达式模式:
(?P.*)(?。+)(?.*)

这个正则表达式应该匹配如下字符串:
你好,我的朋友(敌人)很高兴再次见到你

prefix
组应捕获
hello my

words
小组应捕获
朋友|敌人

postfix
组应该捕获
很高兴再次见到你

此正则表达式还使用lookbehinds检查是否使用字符串中的
\
进行了转义。例如,由于在
)之前存在
\

你好,我的\(朋友|敌人)很高兴再次见到你

你好,我的(朋友|敌人\)很高兴再次见到你

当我使用在线网站检查该模式时,它运行良好,但当我尝试使用python(我使用的是python 3.7)运行时,它会抛出以下错误:
re.错误:缺失),位置35处未终止的子模式

有什么问题

编辑: 以下是我在python中如何使用它:

pattern=“(?P.*)(?.+)(?.*)”
匹配=重新搜索(图案、线条)
@Md Narimani 正如@erhumoro在评论中所建议的,而不是:

line = "hello my (friend|enemy) nice to see you again"
pattern = "(?P<prefix>.*)(?<!\\)\((?P<words>.+)(?<!\\)\)(?P<postfix>.*)"
match = re.search(pattern, line)
line=“你好,我的(朋友|敌人)很高兴再次见到你”
pattern=“(?P.*)(?.+)(?.*)”
匹配=重新搜索(图案、线条)
做:

line=“你好,我的(朋友|敌人)很高兴再次见到你”
图案=r“(?P.*)(?。+)(?.*)”
匹配=重新搜索(图案、线条)

这是因为转义字符有问题。

请发布代码中定义和使用正则表达式的部分。您需要使用原始字符串(
r“…
),以避免转义问题。谢谢。因为我从文件中读取了
,所以它不需要转义。我只需要使用
r
作为
模式。你是对的,让我编辑答案,这样其他人可能会更容易找到答案。
line = "hello my (friend|enemy) nice to see you again"
pattern = r"(?P<prefix>.*)(?<!\\)\((?P<words>.+)(?<!\\)\)(?P<postfix>.*)"
match = re.search(pattern, line)