Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.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,我一直在尝试匹配以下字符串: string = "TEMPLATES = ( ('index.html', 'home'), ('base.html', 'base'))" 但不幸的是,我对正则表达式的了解非常有限,正如您所看到的,有两个括号需要匹配,还有第二个括号中的内容 我尝试使用re.match(“\(w*\)”,string),但它不起作用,任何帮助都将不胜感激。您的示例正在寻找打开的paren,后跟零个或多个字母w,后跟关闭的paren。您可能希望使用\w而不是w,但这在您的情况下无

我一直在尝试匹配以下字符串:

string = "TEMPLATES = ( ('index.html', 'home'), ('base.html', 'base'))"
但不幸的是,我对正则表达式的了解非常有限,正如您所看到的,有两个括号需要匹配,还有第二个括号中的内容
我尝试使用
re.match(“\(w*\)”,string)
,但它不起作用,任何帮助都将不胜感激。

您的示例正在寻找打开的paren,后跟零个或多个字母w,后跟关闭的paren。您可能希望使用\w而不是w,但这在您的情况下无论如何都不起作用,因为在打开的paren旁边有非单词字符


我认为你应该考虑把逗号串在逗号上。您的最终目标是什么?

首先,使用
\(
不足以匹配括号。Python通常会对其字符串中的某些转义序列做出反应,这就是为什么它将
\(
解释为简单的
)。您必须编写
\(
或使用原始字符串,例如
r'(
r)(“

其次,当您使用
re.match
时,您将正则表达式搜索锚定到字符串的开头。如果您想在字符串中的任何位置查找模式,请使用
re.search

正如约瑟夫在他的回答中所说,你想要找到什么并不十分清楚。例如:

string = "TEMPLATES = ( ('index.html', 'home'), ('base.html', 'base'))"
print re.findall(r'\([^()]*\)', string)

编辑:

index.html <-> home
base.html <-> base
我明白了:在这种情况下,转义是不相关的。但是
re.match
re.search
re.findall
相比仍然很重要。

试试这个:

import re
w = "TEMPLATES = ( ('index.html', 'home'), ('base.html', 'base'))"

# find outer parens
outer = re.compile("\((.+)\)")
m = outer.search(w)
inner_str = m.group(1)

# find inner pairs
innerre = re.compile("\('([^']+)', '([^']+)'\)")

results = innerre.findall(inner_str)
for x,y in results:
    print("%s <-> %s" % (x,y))

如果字符串看起来像有效的Python代码,则可以执行以下操作:

import ast
var, s = [part.strip() for part in 
     "TEMPLATES = ( ('index.html', 'home'), ('base.html', 'base'))".split('=')]
result= ast.literal_eval(s)

如果要验证括号是否平衡到两个深度,可以使用以下正则表达式:

import re;

string = """( ('index.html', 'home'), ('base.html', 'base'))
('index.html', 'home')
('base.html', 'base')
"""

pattern = re.compile(r"(?P<expression>\(([^()]*(?P<parenthesis>\()(?(parenthesis)[^()]*\)))*?[^()]*\))")

match = pattern.findall(string)

print(match[0][0])
print(match[1][0])
print(match[2][0])
导入re;
string=“”(('index.html','home'),('base.html','base'))
('index.html','home')
('base.html','base')
"""
pattern=re.compile(r“(?P\([^()]*(?P\()((?(括号)[^()]*\)))*?[^()]*\))
match=pattern.findall(字符串)
打印(匹配[0][0])
打印(匹配[1][0])
打印(匹配[2][0])
此正则表达式使用条件语句
(?(括号)[^()]*\)


演示:

我要做的是匹配字符串“TEMPLATES=(('index.html','home'),('base.html','base')),并用另一个字符串替换它,有没有办法匹配“TEMPLATES=”部分和括号?顺便说一句,感谢您对
重新匹配(\(hello\),(hello)的解释
工作得很好,不过我同意总是使用
r“…”通常比较容易
用于正则表达式文本。@paulo:你想对匹配项做什么,请验证格式?基本上,我正在打开一个django设置文件,匹配一个特定的字符串,并替换它的内容,用于回复,但是否可以匹配整个字符串,包括“TEMPLATES=”保罗:我已经添加了一个与整个字符串匹配的正则表达式。非常感谢你,我非常感谢你的帮助,也感谢所有贡献者:DGood point,@akaRem--
str
是字符串的内置类型名,重新使用该名称可能是不明智的。我已经更新了e回答使用
w
代替
str
@akaRem“当然,许多人没有意识到这些深奥的内置函数甚至是存在的,如果他们被阻止将它们用作变量名,他们会感到惊讶。从这里开始,这只是一条渐进的道路。许多人使用名为str或len的参数编写函数或方法,或者使用诸如compile或format之类的名称编写函数或方法。”
import ast
var, s = [part.strip() for part in 
     "TEMPLATES = ( ('index.html', 'home'), ('base.html', 'base'))".split('=')]
result= ast.literal_eval(s)
import re;

string = """( ('index.html', 'home'), ('base.html', 'base'))
('index.html', 'home')
('base.html', 'base')
"""

pattern = re.compile(r"(?P<expression>\(([^()]*(?P<parenthesis>\()(?(parenthesis)[^()]*\)))*?[^()]*\))")

match = pattern.findall(string)

print(match[0][0])
print(match[1][0])
print(match[2][0])