求解文中正则匹配'\'将''转义成字符串的原理,文中正转义,In [33]: re.
求解文中正则匹配'\'将''转义成字符串的原理,文中正转义,In [33]: re.
In [33]: re.match('ab\\*c','ab*cd')Out[33]: <_sre.SRE_Match object; span=(0, 4), match='ab*c'>
如上,没想明白为什么能匹配到,我的匹配模式中不是使用'\'将''转义成了字符串了吗,为什么最后还能匹配到结果??谢谢!!
不想匹配到就加个 r。
re.match(r'ab\\*c','ab*cd')
'ab\\*c'
这个规则在 compile 之后确实就是
'ab*c' // 这里*表示匹配`*`这个字符
那么当然可以匹配目标字符串 ab*cd 中的 ab*c
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。
编橙之家文章,
相关内容
- Mac升级Python最新版本后有一些模块不能使用是什么原因
- ubuntu卸载python2.7再重装的问题,ubuntu卸载python2.7,ubunt
- Python语言中class使用.bar()与().bar应用不是很明白,pyth
- Django Memcache操作获取值问题,djangomemcache,请问,不同的
- 学过Java想再学一门新的语言,选择python怎么样?,javapy
- python爬虫抓取图片 终端报错 <urlopen error no hsot g
- Python3.4找不到pip.ini配置文件怎么办?,,python3.4自带的
- Python编码声明都有哪几种写法,utf-8编码,pythonutf-8,# -
- Python字符编码不能完全转换想问下为什么,python字符编
- Python排序选择排序、插入排序和希尔排序不是很理解,
评论关闭