美文网首页
python正则使用re模块

python正则使用re模块

作者: 思考的小妮子 | 来源:发表于2019-08-09 13:37 被阅读0次

import re   // 导入正则模块

>>> str1 = 'imooc hello'

>>> pa = re.compile(r'imooc')   //填写正则部分    r表示显示原字符串,不会被转义,否则需要写转义的字符串   先生成pattern对象

>>> str = pa.match(str1)      //匹配并得到结果   

>>> print(str)

<re.Match object; span=(0, 5), match='imooc'>

#匹配多个

>>> str1 = 'Imooc hello Imooc'

>>> pa = re.compile(r'(imooc)', re.I)

>>> str = pa.match(str1)

>>> print(str.groups())   #多个会返回元祖形式 

('Imooc',)

m.groups() 返回所有括号匹配的字符,以tuple格式

相关文章

网友评论

      本文标题:python正则使用re模块

      本文链接:https://www.haomeiwen.com/subject/kgacjctx.html