python - 一個簡單的正則匹配問題
問題描述
In [33]: re.match(’ab*c’,’ab*cd’)Out[33]: <_sre.SRE_Match object; span=(0, 4), match=’ab*c’>
如上,沒想明白為什么能匹配到,我的匹配模式中不是使用’’將’’轉義成了字符串了嗎,為什么最后還能匹配到結果??謝謝!!
問題解答
回答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.
其實也沒看懂你到底要匹配哪種模式,不過你的問題上面的應該可以解決。建議用raw string。
回答2:’ab*c’
這個規則在 compile 之后確實就是
’ab*c’ // 這里*表示匹配`*`這個字符
那么當然可以匹配目標字符串 ab*cd 中的 ab*c
回答3:不想匹配到就加個 r。
re.match(r’ab*c’,’ab*cd’)
相關文章:
1. javascript - 手機點擊input時,button會被頂上去?求解決!!!2. 百度地圖api - Android 百度地圖 集成了定位,導航 相互的jar包有沖突?3. python - django 按日歸檔統計訂單求解4. 網頁爬蟲 - python爬蟲用BeautifulSoup爬取<s>元素并寫入字典,但某些div下沒有這一元素,導致自動寫入下一條,如何解決?5. javascript - vue-mint UI - icon在哪里有文檔?6. HTML5禁止img預覽該怎么解決?7. javascript - vscode alt+shift+f 格式化js代碼,通不過eslint的代碼風格檢查怎么辦。。。8. html5 - 表單無法屏蔽自動填充 autocomplete=off9. html - 請教一個前端css問題。10. 請教一個python字符串處理的問題?
