import re
1.re.match() //只能从头开始匹配
例如:
ret = re.match("abc","123abc123abc")//匹配abc失败
ret =re.match("abc","abc123")//匹配abc成功
ret.span()//返回匹配的索引范围 (0,2)
ret.group()//返回匹配的字符'abc'
2.re.search() //从全字段匹配,匹配一次成功就返回
例如:
re.search("abc","123abc123abc")//匹配abc成功,只找到第一个abc
3.re.findall() //匹配所有
re.findall("abc","123abc123abc")//匹配abc成功,找到2个abc
4.分组匹配,并加上自定义标识:
固定格式(?P<自定义标识>匹配标识)
例如:
ret = re.search("(?P<name>[a-z|A-Z]+)(?P<age>[0-9]+)","123zhangsan23asdf")
print ret.group() //输出“zhangsan23”
print ret.groupdict()//输出{“name”:"zhangsan","age":"23"}
5.re.split()
例如:
ret = re.split("[0-9]+","abc12def34gh")
输出["abc","def","gh"]
6.re.sub("[0-9]+","|","abc12def34gh")
输出"abc|def|gh"
re.sub("[0-9]+","|","abc12def34gh",count=1)
输出"abc|def34gh"
7.匹配忽略大小写
例如
ret = re.search("[a-z]+","abcdABC",flag=re.I)
输出"abcdABC“