高级特性-正则表达式
发布日期:2021-06-30 16:15:17 浏览次数:2 分类:技术文章

本文共 448 字,大约阅读时间需要 1 分钟。

  1. re.search(pattern,string,flag)

与re.match()不同,匹配整个字符串,match如果开始不匹配则返回none

  2. re.sub替换

     re.sub(pattern,replace,string)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/python
 
import 
re
 
#re.sub function testing
 
phone
=
"Cats are smarter than dogs"
 
number
=
re.sub(
'\D'
,"",phone)
 
print
(number)
 
#re.searach function testing
#caution: space in the regex
matchObj
=
re.search(
"(.*) are (.*?) .*"
,phone,re.M|re.I)
if 
matchObj:
   
print
(matchObj.groups())

转载地址:https://kerry.blog.csdn.net/article/details/42643079 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:高级特性-面向对象部分
下一篇:高级特性-操作数据库

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月25日 15时02分27秒