1.1 正则表达式的特点是:
灵活性、逻辑性和功能性非常强;
可以迅速地用极简单的方式达到字符串的复杂控制;
对于刚接触的人来说,比较晦涩难懂。
1.2 re模块常用方法
re.match():
result = re.match("hello", "hellozzy world")
print(result, result.group(), type(result))
在字符串开头匹配'hello',如果匹配成功(可以是空字符串)返回对应的match对象,否则返回None。
re.search():
result = re.search("hello", "2018hellozzy world")
print(result.group())
扫描整个字符串'2018hellozzy world',找到与正则表达式'hello'的第一个匹配(可以是空字符串),并返回一个对应的match对象。
如果 没有匹配返回None.
re.fullmatch:
result = re.fullmatch("hello", "hello1")
print(result)
'hello1'是否整个和'hello'匹配,如果是返回对应的match对象,否则返回None。
re.findall:
result = re.findall("hello", "zzy hello china hello world")
print(result, type(result))
返回结果是一个列表
re.split:
result = re.split("hello", "zzy hello china hello world hello zhengzhou", 2)
print(result, type(result))
返回结果是,分割列表
re.sub:
sub(pattern, repl, string, count=0, flags=0)
使用repl替换pattern匹配到的内容,最多匹配count次
re.iterator:
finditer(pattern, string, flags=0)
返回迭代器
re.compile:
# compile(pattern, flags=0)
pat = re.compile("hello")
print(pat, type(pat))
result = pat.search("helloworld")
print(result, type(result))
编译得到匹配模型
1.3 flags
re.I re.IGNORECASE: 匹配中大小写不敏感
re.M re.MULTILINE: "^"匹配字符串开始以及"\n"之后;"$"匹配"\n"之前以及字符串末尾。通常称为多行模式
re.S re.DOTALL: "."匹配任意字符,包括换行符。通常称为单行模式
如果要同时使用单行模式和多行模式,只需要将函数的可选参数flags设置为re.I | re.S即可。
1.4 单个字符匹配
字符 | 功能 |
. | 匹配任意1个字符(除了\n)(单行模式合一匹配\n) |
[ ] | 匹配[ ]中列举的字符 |
\d | 匹配数字,即0-9 |
\D | 匹配非数字,即不是数字 |
\s | 匹配空白,即 空格,tab键 |
\S | 匹配非空白 |
\w | 匹配单词字符,即a-z、A-Z、0-9、_ |
\W | 匹配非单词字符 |
1.5 匹配多个字符
字符 | 功能 |
* | 匹配前一个字符出现0次或者无限次,即可有可无 |
+ | 匹配前一个字符出现1次或者无限次,即至少有1次 |
? | 匹配前一个字符出现1次或者0次,即要么有1次,要么没有 |
{m} | 匹配前一个字符出现m次 |
{m,} | 匹配前一个字符至少出现m次 |
{m,n} | 匹配前一个字符出现从m到n次 |
1.6 表示边界
字符 | 功能 |
^ | 匹配字符串开头 |
$ | 匹配字符串结尾 |
\b | 匹配一个单词的边界 |
\B | 匹配非单词边界 |
1.7 特殊贪婪字符 ?
正则匹配默认贪婪模式即匹配尽可能多个字符
result = re.findall("he*", "hee heeeee zzheee0")
print(result)
#结果为 ['hee', 'heeeee', 'heee']
当?出现在+、?、*、{m}之后开启非贪婪模式
result = re.findall("he*?", "hee heeeee zzheee0")
print(result)
#结果为 ['h', 'h', 'h']