python - 一個簡單的正則匹配問題
問題描述
In [33]: re.match(’ab*c’,’ab*cd’)Out[33]: <_sre.SRE_Match object; span=(0, 4), match=’ab*c’>
如上,沒想明白為什么能匹配到,我的匹配模式中不是使用’’將’’轉(zhuǎn)義成了字符串了嗎,為什么最后還能匹配到結(jié)果??謝謝!!
問題解答
回答1:Regular expressions use the backslash character (’’) to indicate special forms or to allow special characters to be used without invoking their special meaning. This collides with Python’s usage of the same character for the same purpose in string literals; for example, to match a literal backslash, one might have to write ’’ as the pattern string, because the regular expression must be , and each backslash must be expressed as inside a regular Python string literal.
其實也沒看懂你到底要匹配哪種模式,不過你的問題上面的應(yīng)該可以解決。建議用raw string。
回答2:’ab*c’
這個規(guī)則在 compile 之后確實就是
’ab*c’ // 這里*表示匹配`*`這個字符
那么當(dāng)然可以匹配目標(biāo)字符串 ab*cd 中的 ab*c
回答3:不想匹配到就加個 r。
re.match(r’ab*c’,’ab*cd’)
相關(guān)文章:
1. objective-c - iOS開發(fā)支付寶和微信支付完成為什么跳轉(zhuǎn)到了之前開發(fā)的一個app?2. mysql優(yōu)化 - 關(guān)于mysql分區(qū)3. 請教各位大佬,瀏覽器點 提交實例為什么沒有反應(yīng)4. 致命錯誤: Class ’appfacadeTest’ not found5. javascript - ionic2 input autofocus 電腦成功,iOS手機(jī)鍵盤不彈出6. python - 管道符和ssh傳文件7. css - 移動端字體設(shè)置問題8. css - 求推薦適用于vue2的框架 像bootstrap這種類型的9. html5 - 如何實現(xiàn)帶陰影的不規(guī)則容器?10. javascript - 循環(huán)嵌套多個promise應(yīng)該如何實現(xiàn)?
