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. 在mybatis使用mysql的ON DUPLICATE KEY UPDATE語法實現存在即更新應該使用哪個標簽?2. mysql - 數據庫建字段,默認值空和empty string有什么區別 1103. mysql - 這種分級一對多,且分級不平衡的模型該怎么設計表?4. Navicat for mysql 中以json格式儲存的數據存在大量反斜杠,如何去除?5. mac OSX10.12.4 (16E195)下Mysql 5.7.18找不到配置文件my.cnf6. mysql mysql_real_escape_string() 轉義問題7. 新人求教MySQL關于判斷后拼接條件進行查詢的sql語句8. mysql - 千萬數據 分頁,當偏移量 原來越大時,怎么優化速度9. MySQL FOREIGN KEY 約束報錯10. mysql - 數據庫表中,兩個表互為外鍵參考如何解決
