反斜杠的问题
python中使用正则表达式作为转义字符,这就可能造成反斜杠困扰。假如你在文本中匹配字符'\',那么正则表达式中则需要为'\\\\',前两个和后两个都是用于将反斜杠转义成反斜杠,然后反斜杠再将反斜杠转义成字符串中的反斜杠。
In [38]: r'\\'
Out[38]: '\\\\'
正则表达式用到的主要方法
#返回pattern对象
re.compile(string[,flag])
#以下为匹配所用函数
re.match(pattern, string[, flags])
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])
首先需要了解的是正则表达式的含义: 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑。
pattern是一个模式匹配,就是上文所说的"规则字符串",可以通过re.compile()来获得。还有就是flags参数
- ? re.I(全拼:IGNORECASE): 忽略大小写(括号内是完整写法,下同)
- ? re.M(全拼:MULTILINE): 多行模式,改变'^'和'$'的行为(参见上图)
- ? re.S(全拼:DOTALL): 点任意匹配模式,改变'.'的行为
- ? re.L(全拼:LOCALE): 使预定字符类 \w \W \b \B \s \S 取决于当前区域设定
- ? re.U(全拼:UNICODE): 使预定字符类 \w \W \b \B \s \S \d \D 取决于unicode定义的字符属性
- ? re.X(全拼:VERBOSE): 详细模式。这个模式下正则表达式可以是多行,忽略空白字符,并可以加入注释。
re.match(pattern, string[, flags])
这个方法将会从string(我们要匹配的字符串)的开头开始,尝试匹配pattern,如果匹配不会,则返回为None
In [43]: import re
In [44]: pattern=re.compile(r"hello")
In [45]: re.match(pattern,r'hee hello')
In [46]: re.match(pattern,r'helloiii')
Out[46]: <_sre.SRE_Match object; span=(0, 5), match='hello'>
re.search(pattern, string[, flags])
search方法与match方法极其类似,区别在于match()函数只检测pattern是不是在string的开始位置匹配,search()会扫描整个string查找匹配,match()只有在0位置匹配成功的话才有返回,如果不是开始位置匹配成功的话,match()就返回None。
In [47]: re.search(pattern,r'hee hello')
Out[47]: <_sre.SRE_Match object; span=(4, 9), match='hello'>
In [48]: re.search(pattern,r'helloiii')
Out[48]: <_sre.SRE_Match object; span=(0, 5), match='hello'>
re.split(pattern, string[, maxsplit])
按照能够匹配的子串将string分割后返回列表。maxsplit用于指定最大分割次数,不指定将全部分割。
In [42]: re.split(r'[\s|\\]',r'坑底Z蛙 胡大海\日了狗人了')
Out[42]: ['坑底Z蛙', '胡大海', '日了狗人了']
re.findall(pattern, string[, flags])
搜索string,以列表形式返回全部能匹配的子串。
In [50]: pattern = re.compile(r'\d+')
In [51]: re.findall(pattern,'one1two2three3four4')
Out[51]: ['1', '2', '3', '4']
re.finditer(pattern, string[, flags])
搜索string,返回一个顺序访问每一个匹配结果(Match对象)的迭代器。
In [52]: for m in re.finditer(pattern,'one1one2one3one4'):
...: print(m.group())
...:
1
2
3
4
re.sub(pattern, repl, string[, count])
使用repl替换string中每一个匹配的子串后返回替换后的字符串。
当repl是一个字符串时,可以使用\id或\g、\g引用分组,但不能使用编号0。
当repl是一个方法时,这个方法应当只接受一个参数(Match对象),并返回一个字符串用于替换(返回的字符串中不能再引用分组)。
count用于指定最多替换次数,不指定时全部替换。
In [55]: re.sub(pattern,r'\2 \1', s)
Out[55]: 'say i, world hello!'
In [56]: def func(m):
...: return m.group(1).title() + ' ' + m.group(2).title()
...:
In [57]: re.sub(pattern,func, s)
Out[57]: 'I Say, Hello World!'
re.subn(pattern, repl, string[, count])
返回 (sub(repl, string[, count]), 替换次数)。
In [58]: re.subn(pattern,func, s)
Out[58]: ('I Say, Hello World!', 2)
In [111]: re.match(r'(a)(?(1)b|c)','ab')
Out[111]: <_sre.SRE_Match object; span=(0, 2), match='ab'>
In [117]: re.match(r'(a)?(?(1)b|c)','ab')
Out[117]: <_sre.SRE_Match object; span=(0, 2), match='ab'>
In [115]: re.match(r'(a)?(?(1)b|c)','c')
Out[115]: <_sre.SRE_Match object; span=(0, 1), match='c'>