【摘要】 正则表达式特殊符号和字符表示法描述匹配的表达式res.group()re1|re2匹配正则表达式re1或者re2res=res.search(“foo|bar”,”xxxofooxx”)foo.匹配任意字符(除\n之外)res=re.search(“b.b”,”xxbobxxx”)bob^匹配字符开始res=re.search(“^b.b”,”xxbobxxx”)bob$匹配字符串结尾*0次…
正则表达式特殊符号和字符
|
表示法 |
描述 | 匹配的表达式 | res.group() |
| re1|re2 | 匹配正则表达式re1或者re2 | res=res.search(“foo|bar”,”xxxofooxx”) | foo |
| . | 匹配任意字符(除\n之外) | res=re.search(“b.b”,”xxbobxxx”) | bob |
| ^ | 匹配字符开始 | res=re.search(“^b.b”,”xxbobxxx”) | bob |
| $ | 匹配字符串结尾 | ||
| * | 0次或者多次 | ||
| + | 1次或者多次 |
| ? | 0次或者1次 | ||
| {N} | 匹配n次 | ||
| {M,N} | 匹配m到n次 | ||
| [….] | 匹配来自字符集的任意字符 | ||
| [x-y] | 匹配x到y的任意字符 | ||
| [^…] | […]取非 | ||
| (*|+|{})? | 匹配频繁出现或者重复出现的符号的非贪婪版本 | res=re.search(“.[+|?]?[1-9]”,”ds4b”) | |
| \w | 字母数字下划线 | \W | 非字母数字下划线 |
| \s | 任意空白字符 | \S | 任意非空白字符 |
| \d | 任意数字 | \D | 任意非数字 |
re模块常用的方法
| 函数/方法 | 描述 | 示例 | res.group()/res |
| match(pattern,string,flag=0) | 匹配以给定限制开头的字符串 | ||
| search(pattern,string,flag=0) | 在整个字符串种寻找匹配 | ||
| findall(pattern,string,flag=0) | 查找字符串中所有出现的正则表达式模式,返回列表 | ||
| sub(pattern,string,flag=0) | 将选中的字符串替换为给定的字符串 | ||
| split(pattern,string,maxsplit=0,flags=0) | 根据给定字符串切分字符串 | ||
| complie(pattern,[,flags]) | 编译正则表达式,获得一个正则表达式对象 | ||
| group(num=0) | f返回整个匹配对象,或者编号为num的特定子组 | ||
| groups(default=None) | 返回一个包含所有子组的元组 | ||
| re.i,re.IGNORECASE | 不区分大小写 |
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END






















![[桜井宁宁]COS和泉纱雾超可爱写真福利集-一一网](https://www.proyy.com/skycj/data/images/2020-12-13/4d3cf227a85d7e79f5d6b4efb6bde3e8.jpg)
![[桜井宁宁] 爆乳奶牛少女cos写真-一一网](https://www.proyy.com/skycj/data/images/2020-12-13/d40483e126fcf567894e89c65eaca655.jpg)
