正则表达式的问题 [英] problem with regex
问题描述
我有正则表达式:''[A-Za-z]:\\([^ /:\ * \?"<> \ |])*''
当我这样做时,re.compile(''[A-Za-z]:\\([^ /:\ * \?"<> \ |])*'')...我得到
sre_constants.error:不平衡的括号
我是否需要逃避别的东西?我看到我有匹配的
括号。
thx
abcd写道:
我有正则表达式:''[A-Za-z]:\\ ([^ /:\ * \?"<> \ |])*''
当我这样做时,re.compile(''[A- Za-z]:\\([^ /:\ * \?"<> \ |])*'')...我得到了
sre_constants.error:不平衡的括号
我还需要逃避别的吗?我看到我有匹配的
括号。
你应该使用原始字符串:
re.compile(r''[A-Za-z]:\\ \\\([^ /:\ * \?"<> \ |])*'')
问候,
Rob
2006年7月28日05:45:05 -0700,abcd< co ******* @ gmail.comwrote:
我有正则表达式:''[A-Za-z]:\\([^ /:\ * \?"<> ; \ |])*''
当我这样做时,re.compile(''[A-Za-z]:\\([^ /:\ * \?"<> \ |])*'')...我收到了
sre_constants.error:不平衡的括号
我需要逃避别的吗?我看到我有匹配的
括号。
thx
-
尝试将参数设为原始字符串:
re.compile(r''[A-Za-z]:\\([^ /:\\ \\ * \?"<> \ |])*'')
当我这样做时,重新编译(''[A-Za z]:\\([^ /:\ * \?"<> \ |])*'')...我得到
>
sre_constants.error:不平衡的括号
因为你没有使用原始字符串,所以escapables变为
转义,使你的正则表达式像
[A-Za-z]:\([^ /:\ * \?"< ;> \ |])*
(因为它知道什么是\\,但可能不属于
对\?或\ |的意义,因此只留下它们。
因此,你有\(&;你的正则表达式,这是一个字面值
open-paren。但你有一个),这是一个关闭分组
paren。该错误表示关闭分组。 paren
并没有关闭一些以前开过的人。
一般的好习惯用原始字符串推动所有这些东西:
>
r" [A-Za-z]:\\([^ /:\ * \?"<> \ |])*"
解决了很多头痛问题。
-tkc
I have a regex: ''[A-Za-z]:\\([^/:\*\?"<>\|])*''
when I do, re.compile(''[A-Za-z]:\\([^/:\*\?"<>\|])*'') ...I get
sre_constants.error: unbalanced parenthesis
do i need to escape something else? i see that i have matching
parenthesis.
thx
abcd wrote:I have a regex: ''[A-Za-z]:\\([^/:\*\?"<>\|])*''
when I do, re.compile(''[A-Za-z]:\\([^/:\*\?"<>\|])*'') ...I get
sre_constants.error: unbalanced parenthesis
do i need to escape something else? i see that i have matching
parenthesis.You should use raw string:
re.compile(r''[A-Za-z]:\\([^/:\*\?"<>\|])*'')
Regards,
Rob
On 28 Jul 2006 05:45:05 -0700, abcd <co*******@gmail.comwrote:I have a regex: ''[A-Za-z]:\\([^/:\*\?"<>\|])*''
when I do, re.compile(''[A-Za-z]:\\([^/:\*\?"<>\|])*'') ...I get
sre_constants.error: unbalanced parenthesis
do i need to escape something else? i see that i have matching
parenthesis.
thx
--Try making the argument a raw string:
re.compile(r''[A-Za-z]:\\([^/:\*\?"<>\|])*'')
when I do, re.compile(''[A-Za-z]:\\([^/:\*\?"<>\|])*'') ...I get>
sre_constants.error: unbalanced parenthesis
Because you''re not using raw strings, the escapables become
escaped, making your regexp something like
[A-Za-z]:\([^/:\*\?"<>\|])*
(because it knows what "\\" is, but likely doesn''t attribute
significance to "\?" or "\|", and thus leaves them alone).
Thus, you have "\(" in your regexp, which is a literal
open-paren. But you have a ")", which is a "close a grouping"
paren. The error is indicating that the "close a grouping" paren
doesn''t close some previously opened paren.
General good practice shoves all this stuff in a raw string:
r"[A-Za-z]:\\([^/:\*\?"<>\|])*"
which solves much of the headache.
-tkc
这篇关于正则表达式的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!