1、正则表达式的概念
正则表达式就是使用一些特定的表达式,来表示符合其规范的字符串。
2、常用操作符
1 | ^:^ab,代表着必须是ab开头,[^ab]代表不能是a或b开头 |
3、常用方法
1)search():会在指定的字符串中查找,是否有匹配正则表达式的内容
1 | 函数格式:search(pattern, str) |
2)match():会从字符串的开始进行匹配
1 | 函数格式:match(pattern, str) |
3)findall():会从指定的字符串中,匹配所有满足正则表达式的内容
1 | 函数格式:findall(pattern,str) |
4)split():根据字符串匹配正则表达式的情况来切分
1 | 函数格式:split(pattern, str, maxsplit) |
5)finditer():根据字符串匹配正则表达式,返回一个迭代对象,其中的每一个对象都是match对象
1 | 函数格式:finditer(pattern,str) |
6)sub():根据正则表达式,将指定的字符串内容替换
1 | 函数格式:sub(pattern, repl, string, count) |
7)compile():生成正则表达式对象
1 | 函数格式:compile(pattern) |
4、match对象
1)match对象属性
1 | string:待匹配的字符串 |
2)match方法
1 | group(0):获得匹配后的字符串 |