|
一、使用match()方法匹配字符串
match方法从字符串的起始部分进行匹配,匹配成功,返回,匹配失败返回None
ep:
1
2
3
4
5
6
| >>> m = re.match('foo','food') ##success
>>>
>>>
>>> if m is not None: m.group()
...
'foo'
|
匹配成功返回foo
1
2
3
4
5
| >>> m = re.match('foo','seafood') ##False
>>>
>>> if m is not None: m.group()
...
>>>
|
匹配失败返回None
二、使用search()方法匹配字符串
search()方法会从字符串的任意位置进行匹配,匹配成功返回,匹配失败返回None
1
2
3
4
5
| >>> m = re.search('foo','seafood') ##success
>>>
>>> if m is not None: m.group()
...
'foo'
|
三、匹配多个字符串
1
2
3
4
5
6
| >>> bt = "bat|bet|bit"
>>> m = re.match(bt,"bat")
>>> if m is not None: m.group()
...
'bat'
>>>
|
匹配成功
1
2
3
4
| >>> m = re.match(bt,"He bit me")
>>> if m is not None: m.group()
...
>>>
|
匹配失败
1
2
3
4
| >>> m = re.search(bt,"He bit me")
>>> if m is not None: m.group()
...
'bit'
|
匹配成功
四、匹配任意单个字符
1
2
3
4
5
| >>> anyend = ".end"
>>> m = re.match(anyend,"bend")
>>> if m is not None: m.group()
...
'bend'
|
匹配成功
1
2
3
4
5
| >>> m = re.search(".end","Tome end")
>>>
>>> if m is not None: m.group()
...
' end'
|
匹配成功
如果是真正的小数点,使用\.
五、字符集 ([ ])
1
2
3
4
| >>> m = re.match('[cr][23][dp][o2]','c2po')
>>> if m is not None: m.group()
...
'c2po'
|
成功匹配c2po
1
2
3
4
5
| >>> m = re.match('r2d2|c3po','r2d2')
>>>
>>> if m is not None: m.group()
...
'r2d2'
|
成功匹配r2d2
六、重复、特殊字符以及分组
1
2
3
4
5
6
| >>> patt = "\w+@(\w+\.)*\w+\.com"
>>> m = re.match(patt,'abc@staff.sina.cn.com')
>>>
>>> if m is not None: m.group()
...
'abc@staff.sina.cn.com'
|
上面的正则表达式完成对邮箱的匹配。
匹配字母+数字
1
2
3
4
| >>> m = re.match('\w\w\w-\d\d\d','abc-123')
>>> if m is not None: m.group()
...
'abc-123'
|
或者
1
2
3
4
| >>> m = re.match('(\w)+-(\d)+','abc-123')
>>> if m is not None: m.group()
...
'abc-123'
|
子组:使用圆括号来匹配和保存数组
groups()方法:匹配包含所有子组的元组。
1
2
3
4
5
6
7
8
9
10
| >>> m = re.match('(\w\w\w)-(\d\d\d)','abc-123')
>>> if m is not None: m.group(1) #子组1
...
'abc'
>>> if m is not None: m.group(2) #子组2
...
'123'
>>> if m is not None: m.groups() #全部子组
...
('abc', '123')
|
由上面可知:
group(1)代表子组1,group(2)代表子组2
groups()获取一个包含所有匹配子组的元组
七、匹配字符串的起始和结尾以及单词边界
1
2
3
| >>> m = re.search(r'\bthe',"this the dog ") #r作用指定是正则表达式
>>> m.group()
'the'
|
匹配the单词
1
2
3
| >>> m = re.search(r'\Bthe',"bitthes the dog ")
>>> m.group()
'the'
|
\B与之相反,匹配单词中的the
八、使用findall()以及finditer()
findall():以列表的形式返回字符串
1
2
| >>> re.findall('a','this is a car and a shu')
['a', 'a', 'a', 'a']
|
出现了四次a,打印四个a的列表
1
2
3
4
5
6
7
8
9
10
11
| >>> s
'This and that'
>>> re.findall(r'(th\w+) and (th\w+)', s, re.I ) #re.I忽略大小写
[('This', 'that')]
finditer:
>>> re.finditer(r'(th\w+) and (th\w+)', s, re.I ).next().groups()
('This', 'that')
>>> re.finditer(r'(th\w+) and (th\w+)', s, re.I ).next().group(1)
'This'
>>> re.finditer(r'(th\w+) and (th\w+)', s, re.I ).next().group(2)
'that'
|
finditer():使用了迭代的方式进行检索,并把结果输出出来。
九、使用sub及subn进行搜索与替换
1
2
| >>> re.sub('a','A',"This is a apple")
'This is A Apple'
|
将a替换成A
1
2
| >>> re.subn('a','A',"This is a apple")
('This is A Apple', 2)
|
使用subs替换过之后,还将替换的个数打印出来。
匹配对象的group除了能够取出匹配分组的编号之外,还可以使用\N,其中N是分组中的编号:
1
2
| >>> re.sub(r'(\d{1,2})/(\d{1,2})/(\d{2}|\d{4})',r'\2/\1/\3','2/20/91' )
'20/2/91'
|
例子中\2 \1 \3分别代表三个分组
十、split分割字符串
1
2
| >>> re.split(':','a:b:c')
['a', 'b', 'c']
|
十一、compile模块
compile创建模式对象
1
2
3
4
| >>> pat=re.compile('A')
>>> m=pat.search('CBA')
>>> m.group()
'A'
|
|
|