2017-03-25 27 views
2

有人可以告訴我爲什麼這個正則表達式在online正則表達式工作正常,但不是在Python中使用re.compile()時。Python的正則表達式錯誤:組名中的壞字符

我已經使用這個網站: https://regex101.com/和測試字符串是:

"test": "value" 

Python代碼

x = r'((?(?=")(?:"(?(?<=\\)(?:.)|(?:[^")]))+")|(?:\w+)))(:|~)\s+((?(?=")(?:"(?(?<=\\)(?:.)|(?:[^"]))+")|(?:\w+)))' 
re.compile(x) 

錯誤:

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "C:\Python27\lib\re.py", line 190, in compile 
    return _compile(pattern, flags) 
    File "C:\Python27\lib\re.py", line 245, in _compile 
    raise error, v # invalid expression 
sre_constants.error: bad character in group name 
+0

您在您的正則表達式中使用條件,它們不被python re語法支持。當您測試模式時,您應該在regex101中切換到python。 –

+0

如果您將regex101切換到Python模式,它也會告訴您它已損壞。 – jonrsharpe

+0

@SebastianProske謝謝,明白了。 –

回答