首页 / 正则 / 正则表达式_合集下(后续还会有补充)
正则表达式_合集下(后续还会有补充)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了正则表达式_合集下(后续还会有补充),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5187字,纯文字阅读大概需要8分钟。
内容图文
![正则表达式_合集下(后续还会有补充)](/upload/InfoBanner/zyjiaocheng/1147/84a42284ee13483298c9f1d6bbcaab08.jpg)
re中引用分组匹配字符串
import
re
#
能够完成对正确的字符串的匹配
ret = re.match("<[a-zA-Z]*>\w*</[a-zA-Z]*>", "<html>hh</html>")
print(ret.group())
# 如果遇到非正常的html格式字符串,匹配出错</htmlbalabala>会一起输出
ret = re.match("<[a-zA-Z]*>\w*</[a-zA-Z]*>", "<html>hh</htmlbalabala>")
#
print(ret.group())
# 正确的理解思路:如果在第一对<>中是什么,按理说在后面的那对<>中就应该是什么# 通过引用分组中匹配到的数据即可,但是要注意是元字符串,即类似 r""这种格式
ret = re.match(r"<([a-zA-Z]*)>\w*</\1>", "<html>hh</html>")
# </\1>匹配第一个规则print(ret.group())
# 因为2对<>中的数据不一致,所以没有匹配出来
test_label = "<html>hh</htmlbalabala>"
ret = re.match(r"<([a-zA-Z]*)>\w*</\1>", test_label)
if ret:
print(ret.group())
else:
print("%s 这是一对不正确的标签" % test_label)
‘‘‘
<html>hh</html>
<html>hh</htmlbalabala>
<html>hh</html>
<html>hh</htmlbalabala> 这是一对不正确的标签
‘‘‘
re中引用分组匹配字符串_2
import
re
labels = ["<html><h1>www.itcast.cn</h1></html>", "<html><h1>www.itcast.cn</h2></html>"]
for label in labels:
ret = re.match(r"<(\w*)><(\w*)>.*</\2></\1>", label)
# <\2>和第二个匹配一样的内容if ret:
print("%s 是符合要求的标签" % ret.group())
else:
print("%s 不符合要求" % label)
‘‘‘
<html><h1>www.itcast.cn</h1></html> 是符合要求的标签
<html><h1>www.itcast.cn</h2></html> 不符合要求
‘‘‘
re中提取区号和电话号码
import
re
ret = re.match("([^-]*)-(\d+)","010-1234-567")
# 除了 - 的所有字符
# 对最后一个-前面的所有字符进行分组,直到最后一个数字为止print(ret.group( ))
print(ret.group(1))#返回-之前的数据,不一定是最后一个-之前print(ret.group(2))
re中的贪婪
import
re
s= "This is a number 234-235-22-423"
r=re.match(".+(\d+-\d+-\d+-\d+)",s)
# .+ 尽量多的匹配任意字符,匹配到-前一个数字之前
# . 匹配任意字符print(type(r))
print(r.group())
print(r.group(0))
print(r.group(1))
r=re.match(".+?(\d+-\d+-\d+-\d+)",s)
print(r.group())
print(r.group(1))#到数字停止贪婪‘‘‘
<class ‘re.Match‘>
This is a number 234-235-22-423
This is a number 234-235-22-423
4-235-22-423
This is a number 234-235-22-423
234-235-22-423
‘‘‘
import
re
ret = re.match(r"aa(\d+)","aa2343ddd")
# 尽量多的匹配字符print(ret.group())
# 使用? 将re贪婪转换为非贪婪
ret = re.match(r"aa(\d+?)","aa2343ddd")
# 只输出一个数字print(ret.group())
‘‘‘
aa2343
aa2
‘‘‘
re使用split切割字符串
import
re
ret = re.split(r":| ","info:XiaoLan 22 Hany.control")
# | 或 满足一个即可print(ret)
str1 = ‘one,two,three,four‘
pattern = re.compile(‘,‘)
# 按照,将string分割后返回print(pattern.split(str1))
# [‘one‘, ‘two‘, ‘three‘, ‘four‘]
str2 = ‘one1two2three3four‘print(re.split(‘\d+‘,str2))
# [‘one‘, ‘two‘, ‘three‘, ‘four‘]
re匹配中subn,进行替换并返回替换次数
import
re
pattern = re.compile(‘\d+‘)
strs = ‘one1two2three3four‘print(pattern.subn(‘-‘,strs))
# (‘one-two-three-four‘, 3) 3为替换的次数
re匹配中sub将匹配到的数据进行替换
#
import re
#
ret = re.sub(r"\d+", ‘替换的字符串998‘, "python = 997")
#
# python = 替换的字符串998
#
print(ret)
#
# 将匹配到的数据替换掉,替换成想要替换的数据
#
re.sub("规则","替换的字符串","想要替换的数据")
import
re
def
add(temp):
strNum = temp.group()
# 匹配到的数据.group()方式print("原来匹配到的字符:",int(temp.group()))
num = int(strNum) + 5 #字符串强制转换return str(num)
ret = re.sub(r"\d+", add, "python = 997")
# re.sub(‘正则规则‘,‘替换的字符串‘,‘字符串‘)print(ret)
ret = re.sub(r"\d+", add, "python = 99")
print(ret)
pattern = re.compile(‘\d‘)
str1 = ‘one1two2three3four‘print(pattern.sub(‘-‘,str1))
# one-two-three-fourprint(re.sub(‘\d‘,‘-‘,str1))
# one-two-three-four‘‘‘
原来匹配到的字符: 997
python = 1002
原来匹配到的字符: 99
python = 104
one-two-three-four
one-two-three-four
‘‘‘
re匹配的小例子
import
re
src="https://rpic.douyucdn.cn/appCovers/2016/11/13/1213973_201611131917_small.jpg"
ret = re.search(r"https://.*?\.jpg", src)
print(ret.group())
res = re.compile(‘[a-zA-Z]{1}‘)
strs = ‘123abc456‘print(re.search(res,strs).group( ))
print(re.findall(res,strs)) #findall返回列表元素对象不具有group函数
# print(re.finditer(res,strs)) #返回迭代器对象‘‘‘
https://rpic.douyucdn.cn/appCovers/2016/11/13/1213973_201611131917_small.jpg
a
[‘a‘, ‘b‘, ‘c‘]
‘‘‘
匹配前一个字符出现m次
import
re
src="https://rpic.douyucdn.cn/appCovers/2016/11/13/1213973_201611131917_small.jpg"
ret = re.search(r"https://.*?\.jpg", src)
print(ret.group())
res = re.compile(‘[a-zA-Z]{1}‘)
strs = ‘123abc456‘print(re.search(res,strs).group( ))
print(re.findall(res,strs)) #findall返回列表元素对象不具有group函数
# print(re.finditer(res,strs)) #返回迭代器对象‘‘‘
https://rpic.douyucdn.cn/appCovers/2016/11/13/1213973_201611131917_small.jpg
a
[‘a‘, ‘b‘, ‘c‘]
‘‘‘
引用分组
import
re
strs = ‘hello 123,world 456‘
pattern = re.compile(‘(\w+) (\d+)‘)
# for i in pattern.finditer(strs):
# print(i.group(0))
# print(i.group(1))
# print(i.group(2))#当存在第二个分组时‘‘‘hello 123
hello
123
world 456
world
456
‘‘‘print(pattern.sub(r‘\2 \1‘,strs))
# 先输出第二组,后输出第一组print(pattern.sub(r‘\1 \2‘,strs))
当findall遇到分组时,只匹配分组
import
re
pattern = re.compile(‘([a-z])[a-z]([a-z])‘)
strs = ‘123abc456asd‘# print(re.findall(pattern,strs))
# [(‘a‘, ‘c‘), (‘a‘, ‘d‘)]返回分组匹配到的结果
result = re.finditer(pattern,strs)
for i in result:
print(i.group( )) #match对象使用group函数输出print(i.group(0))#返回匹配到的所有结果print(i.group(1))#返回第一个分组匹配的结果print(i.group(2))#返回第二个分组匹配的结果
# <re.Match object; span=(3, 6), match=‘abc‘>
# <re.Match object; span=(9, 12), match=‘asd‘>
# 返回完整的匹配结果‘‘‘
abc
abc
a
c
asd
asd
a
d
‘‘‘
2020-05-07
原文:https://www.cnblogs.com/hany-postq473111315/p/12845485.html
内容总结
以上是互联网集市为您收集整理的正则表达式_合集下(后续还会有补充)全部内容,希望文章能够帮你解决正则表达式_合集下(后续还会有补充)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。