01 概 念
如果要处理的文件比较简单,直接使用字符串匹配就可以了,如下:

那么使用字符串匹配的python代码:
def find_start_re(fname):
f = open(fname,'r')
for line in f:
if line.startswith('re'):
print(line)
f.close()
find_start_re('re.txt')
执行结果:

单个处理数据和文本,效率不高,不适合数据量大的场景,正则表达式可以解决处理数据量大的情况。
正则表达式概念:
是对字符串操作的一种逻辑公式
应用场景:处理文本和数据
正则表达式过程:依次拿出表达式和文本中的字符比较,如果每一个字符都能匹配,则匹配成功,否则匹配失败
02 re 模块
re使用的过程:

使用举例:




3.匹配时忽略大小写,增加re.I

4.正则表达式语法


*的用法:

+的用法:匹配合理的变量名(必须以‘_’或者字母开头)

?用法:匹配0-99的数字

{m}/{m,n}的用法:匹配合理的126邮箱(@126.com之前是6到10位字符)

*?/+?/??的用法:

5、正则表达式进行边界匹配

6、正则表达式进行分组匹配

| 的用法:匹配0-99的数字


(ab)的用法:

<number>的用法:处理xml文件时会经常用到 python


(?P)、(?P=name)的用法:

03 re的其他方法
因为re模块的match方法是从头开始匹配的,当需要匹配字符串中的子串的时候就不好用了。
 
search方法:
匹配字符串中的数字 ‘study python = 1000’

findall方法:


sub方法:
参数repl有两种方式,一种是本身就是字符串,一种是函数,取函数返回的字符串

split用法:

现在我邀请你进入我们的软件测试学习交流群:【746506216 】,备注“入群”, 大家可以一起探讨交流软件测试,共同学习软件测试技术、面试等软件测试方方面面,还会有免费直播课,收获更多测试技巧,我们一起进阶Python自动化测试/测试开发,走向高薪之路。
资源分享
最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走…

?这些资料,对于想从事【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。希望对大家有所帮助…….

|