2013-10-11 43 views
0

我试图获得一个正则表达式来匹配“[stuff]”。可以有转义字符,但只要有偶数字符,他们什么也不做。我的正则表达式是:正则表达式无法正常工作Python

(?<!\\\\)(\\\\\\\\)*\".*?(?<!\\\\)(\\\\\\\\)*\" 

并在第二次试举有偶数个\ S,也就是说,它会匹配("a daf asd \\"

,但不是在第一次报价做(即\\" adf "

任何人有任何想法,为什么这不起作用?

+3

我希望你使用原始字符串字面量来定义你的正则表达式。 –

回答

0

尝试使用以下内容,如果我清楚你正在尝试做什么。

(?<!\\)(?:\\\\)*".*?(?<!\\)(?:\\\\)*" 

正则表达式的解释:

(?<!\\) Matches if the preceding char is not a backslash 
(?:\\\\)* Matches 0 or more occurrences of two backslashes 
"   Matches a double quote 
.*?  Matches any character except \n (0 or more times) 

Repeat lines 1-3 

live demo

相关问题