正则表达式的不平衡括号错误 [英] Unbalanced parenthesis error with Regex
问题描述
我正在使用以下正则表达式从以下字符模式中包含的网站Javascript数据源获取所有数据
I am using the following regex to obtain all data from a website Javascript data source that is contained within the following character pattern
[[]]);
我正在使用的代码是这样:
The code I am using is this:
regex = r'\[\[.*?\]]);'
match2 = re.findall(regex, response.body, re.S)
print match2
这引发了以下错误消息:
This is throwing up an error message of:
raise error, v # invalid expression
sre_constants.error: unbalanced parenthesis
我认为这是由我的正则表达式内的右括号引起的,这是相当安全的.如何在不出现此错误的情况下定义所需的正则表达式?
I think I am fairly safe in assuming that this is being caused by the closing bracket within my regex. How can I define the regex that I want without getting this error?
谢谢
推荐答案
您还需要转义最后一个括号.字符类外部的方括号不必转义:
You need to escape those last parenthesis as well. Close square brackets outside a character class do not have to be escaped:
regex = r'\[\[.*?]]\);'
^
如果您要获取方括号之间的内容,请在此处使用捕获组.
If you are trying to obtain the content between the square brackets, use a capturing group here.
>>> import re
>>> s = 'foo [[bar]]); baz [[quz]]); not [[foobar]]'
>>> matches = re.findall(r'\[\[(.*?)]]\);', s, re.S)
>>> matches
['bar', 'quz']
这篇关于正则表达式的不平衡括号错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!