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. html5 - javascript写业务有用到什么编程范式没?2. javascript - 一排三个框,各个框的间距是15px,距离外面的白框间距也是15px,这个css怎么写?3. javascript - vue 手机端项目在进入主页后 在进入子页面,直接按返回出现空白情况4. javascript - nodejs调用qiniu的第三方资源抓取,返回401 bad token,为什么5. html5 - vue-cli 装好了 新建项目的好了,找不到项目是怎么回事?6. javascript - immutable配合react提升性能?7. python3.x - python 中的maketrans在utf-8文件中该怎么使用8. javascript - jQuery post()方法,里面的请求串可以转换为GBK编码么?可以的话怎样转换?9. javascript - H5或者JS如何获得当前位置地理定位,只需要获取经纬度即可10. mysql - C#连接数据库时一直这一句出问题int i = cmd.ExecuteNonQuery();

网公网安备