美文网首页
python 正则表达式

python 正则表达式

作者: 青铜搬砖工 | 来源:发表于2018-04-05 10:50 被阅读0次

正则表达式

‘.’ 匹配除了换行符之外的任意字符
‘^’ 以……为开始
‘$’ 以……为结束
‘+’ 包含一个或多个字符
‘*’ 包含0个或多个字符
‘?’包含0个或1个字符
‘+?,*?,??’ python中默认的匹配原则是贪婪的(+,*, ?默认都是贪婪匹配),即尽可能多的匹配到符合要求的字符。(+,*, ?)后面加上?后就变成非贪婪匹配,即尽可能匹配少的符合要求的字符。(贪婪与非贪婪的前提都是要可以匹配到符合正则表达式的字符)。
{m} 匹配重复m次的字符,如a{6}则可以匹配到 :aaaaaa
{m,n} 匹配重复m~n个的字符,如a{1,4},可以匹配:a,aa,aaa,aaaa
{m,n}?用非贪婪匹配m~n个字符,如aaaaaa需要匹配a{1,4}?则只能匹配一个a
[] 匹配括号中的任意一个字符,如[a~z]则可以匹配26个英文字母

re 常用方法

  • re.match(pattern, string[, flags])
import re
pattern = re.compile(r'(one)(one)')
result =  re.match(pattern, 'oneone2three3four4')
print result.group()
print result.groups()
#output
# group() :oneone
#groups():('one', 'one')

  • re.search(pattern, string[, flags])
  • re.split(pattern, string[, maxsplit])
  • re.findall(pattern, string[, flags])
  • re.finditer(pattern, string[, flags])
  • re.sub(pattern, repl, string[, count])
  • re.subn(pattern, repl, string[, count])

相关文章

  • 正则表达式

    Python正则表达式初识(一) Python正则表达式初识(二) Python正则表达式初识(三) Python...

  • 正则表达式

    Python:正则表达式Python:正则表达式

  • Python正则表达式指南

    Python正则表达式指南 本文介绍了Python对于正则表达式的支持,包括正则表达式基础以及Python正则表达...

  • Python爬虫(十)_正则表达式

    本篇将介绍python正则表达式,更多内容请参考:【python正则表达式】 什么是正则表达式 正则表达式,又称规...

  • python正则表达式

    本篇将介绍python正则表达式,更多内容请参考:【python正则表达式】 什么是正则表达式 正则表达式,又称规...

  • [转]python正则表达式(一) 函数使用

    原文:python | 史上最全的正则表达式 更全的正则表达式处理函数:在python中使用正则表达式(一) 0....

  • Python正则表达式

    python正则表达式

  • Python正则表达式用法详解

    搞懂Python 正则表达式用法 Python 正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一...

  • Python正则表达式指南

    本文介绍了Python对于正则表达式的支持,包括正则表达式基础以及Python正则表达式标准库的完整介绍及使用示例...

  • Python处理正则表达式超时的办法

    title: Python处理正则表达式超时的办法tags: [python3, 正则表达式超时, re模块]da...

网友评论

      本文标题:python 正则表达式

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