国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

python - 一個簡單的正則匹配問題

瀏覽:131日期:2022-09-16 18:06:01

問題描述

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’)

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 得荣县| 河西区| 民勤县| 鄂托克前旗| 集安市| 瑞金市| 肇源县| 安阳市| 库尔勒市| 邢台市| 阿巴嘎旗| 铁力市| 宝坻区| 沙田区| 扶风县| 富源县| 新干县| 哈巴河县| 大余县| 阿拉尔市| 西平县| 突泉县| 星子县| 广灵县| 黄山市| 巴青县| 千阳县| 乐山市| 牙克石市| 南康市| 长沙市| 临汾市| 万全县| 霍林郭勒市| 玉溪市| 登封市| 浪卡子县| 祁阳县| 土默特右旗| 宜君县| 白城市|