python - 一個(gè)簡(jiǎn)單的正則匹配問題
問題描述
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.
其實(shí)也沒看懂你到底要匹配哪種模式,不過你的問題上面的應(yīng)該可以解決。建議用raw string。
回答2:’ab*c’
這個(gè)規(guī)則在 compile 之后確實(shí)就是
’ab*c’ // 這里*表示匹配`*`這個(gè)字符
那么當(dāng)然可以匹配目標(biāo)字符串 ab*cd 中的 ab*c
回答3:不想匹配到就加個(gè) r。
re.match(r’ab*c’,’ab*cd’)
相關(guān)文章:
1. mysql - 分庫(kù)分表、分區(qū)、讀寫分離 這些都是用在什么場(chǎng)景下 ,會(huì)帶來(lái)哪些效率或者其他方面的好處2. 圖片鏈接的地址怎么獲得的3. python - 我在使用pip install -r requirements.txt下載時(shí),為什么部分能下載,部分不能下載4. mysql - 如何減少使用或者不用LEFT JOIN查詢?5. mysql - jdbc的問題6. mysql - eclispe無(wú)法打開數(shù)據(jù)庫(kù)連接7. mysql 5個(gè)left關(guān)鍵 然后再用搜索條件 幾千條數(shù)據(jù)就會(huì)卡,如何解決呢8. 視頻文件不能播放,怎么辦?9. mysql - 千萬(wàn)級(jí)數(shù)據(jù)的表,添加unique約束,insert會(huì)不會(huì)很慢?10. html5 - H5 audio 微信端 在IOS上不能播放音樂
