![](/img/trans.png)
[英]How to Replace 2 Backslashes Between $ Symbols with 4 Backslashes using Python and Regex?
[英]How to capture backslashes with regex
我在CSS樣式表中有以下content: "\\f215";
。 我需要從中提取f215
,然后嘗試了以下操作:
re.compile("content: \"\\([a-z0-9]+)\";")
我已經在http://www.pythonregex.com/上對此進行了測試,它的工作原理很吸引人。 但是,當我將其粘貼到腳本中並在本地運行時,會引發sre_constants.error: unbalanced parenthesis
,我不知為什么。 我也試過
re.compile(re.escape('content: "\([a-z0-9]+)";')
re.compile('content: "\\\([a-z0-9]+)";')
但兩者都不匹配。
任何幫助深表感謝!
您需要將模式定義為原始字符串。
re.compile(r'content: "\\([a-z0-9]+)";')
>>> s = r'content: "\f215";'
>>> m = re.compile(r'content: "\\([a-z0-9]+)";')
>>> m.search(s).group(1)
'f215'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.