zl程序教程

您现在的位置是:首页 >  前端

当前栏目

正则表达式_合集下(后续还会有补充)

正则表达式 合集 补充 后续 还会
2023-09-11 14:21:27 时间
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-four

print(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