Python期末复习-正则表达式

262 阅读1分钟

转义字符&失效

  1. 字符串前加r
  2. 转义字符\

字符

  1. []匹配括号内任一字符,不可为空
  2. -表示范围
  3. ^表示否定
  4. 可组合使用

预定义元字符

'.':除\n外任何字符

\d:数字 <=> [0-9]

\s:任何空白字符 \t\n

\w:数字字母下划线 <=> [a-zA-Z0-9_]

\D\S\W表示上面3个的否定

边界匹配符

^:字符串

$:字符串

\b:单词头或尾

\B:不是(单词头且单词尾)

重复限定符

X{n, m}: 重复[n,m]次

x{n,}: 重复>=n次

x{n}:重复n次

x+: 重复>0次

x*: 重复>=0次

x?: 重复0或1次

贪婪算法与懒惰算法

>>> s = 'apple banana,peach , 345 point6 7p,epp8,pear peach'
>>> import re
>>> re.findall(s, r"\bp.*\b")
[]
>>> re.compile(r"\bp.*\b").findall(s)
['peach , 345 point6 7p,epp8,pear peach']
>>> re.compile(r"\bp[a-z]*\b").findall(s)
['peach', 'pear', 'peach']
>>> re.compile(r"\bp.*?\b").findall(s)
['peach', 'point6', 'pear', 'peach']
>>> 

分组符

( | ),类似于或运算

re模块

静态方法 re.xxx

  1. findall(pattern, str[, flag]) 返回列表
  2. match(pattern, str[, flag]) 返回match对象 必须从串首匹配
  3. search(pattern, str[, flag]) 返回match对象
  4. split(pattern, str[, maxsplit]) 返回字符串
  5. sub(pattern, new, str[, count]) 返回字符串
  6. subn(pattern, new, str[,count]) 返回元组(字符串,替换次数)

match对象

group(), 默认参数为0

span(),

start()

end()

compile正则表达式对象

用法和re静态方法类似,只是没有pattern参数