0
点赞
收藏
分享

微信扫一扫

基于Yolov5的检测系统实战

Brose 04-11 16:00 阅读 2

参考资料:python网络爬虫技术与应用【邓维】

1、match()

        从字符串头部开始匹配字符。

import re
content="The123456ismyonephonenumber."
# 字符串长度
print(len(content)) 
# 使用match匹配,第一个参数为正则表达式,第二个参数为要匹配的字符串
result=re.match(r'^The',content)
print(result)
# 输出匹配内容
print(result.group())
# 输出匹配内容的位置索引
print(result.span())

2、search()

        与match()方法不同,search()方法不需要从头开始匹配。

import re
content="OtherThe123456ismyonephonenumber."
result=re.search(r"The.*?(\d+).*?number.",content)
print(result.group())

3、findall()

        match()方法和search()方法都是返回匹配到的第一个内容就结束匹配,而findall()方法是返回全部符合匹配规则的内容,返回的是一个列表。

import re
text="pyypppyyyyypppp"
pattern="py"
for match in re.findall(pattern,text):
    print("Found{!r}".format(match))

4、sub()

        去除或替换匹配的字符。假如写sub("\d+","-"),则是把匹配的内容调换成“-”,例子如下:

import re
content='54abc59de335f7778888g'
content=re.sub("\d+","",content)
print(content)
举报

相关推荐

0 条评论