我有这个正则表达式模式:(?P<prefix>.*)(?<!\\)\((?P<words>.+)(?<!\\)\)(?P<postfix>.*)
这个正则表达式应该匹配如下字符串:hello my (friend|enemy) nice to see you again
prefix
组应该捕获hello my
。words
组应捕获friend|enemy
。postfix
组应该捕获nice to see you again
这个正则表达式还使用lookbehinds检查是否在字符串中使用\
对(
和)
进行转义。例如,由于在(
和)
之前有一个\
,所以这两个样本不应该被检测到:hello my \(friend|enemy) nice to see you again
hello my (friend|enemy\) nice to see you again
当我使用在线网站检查它时,这个模式运行得很好,但是当我尝试用python运行时(我使用的是python3.7),它会抛出以下错误:re.error: missing ), unterminated subpattern at position 35
有什么问题?你知道吗
编辑: 下面是我在python中如何使用它:
pattern = "(?P<prefix>.*)(?<!\\)\((?P<words>.+)(?<!\\)\)(?P<postfix>.*)"
match = re.search(pattern, line)
@Md纳里马尼 正如@erhumoro在评论中建议的,而不是:
执行:
这是因为转义字符的问题。你知道吗
相关问题 更多 >
编程相关推荐