import re
# 部分字符代替
print("hello,world".replace("w","W"))
# hello,World
# find()只发现第一个就返回下标
print("hello,world,hahhad".find("d"))
# 10
# findall()将发现的所有的保存在数组中返回,虾面是匹配一个一个数字返回
print(re.findall("\d", "hhh123sjkdfh34hsbfdh444hsdbfhj88dfdsb323"))
# ['1', '2', '3', '3', '4', '4', '4', '4', '8', '8', '3', '2', '3']
# 只要有一个数字就返回,多数字
print(re.findall("\d+","hhh123sjkdfh34hsbfdh444hsdbfhj88dfd3sb323"))
# ['123', '34', '444', '88', '3', '323']
# 是0个数字的地方返回空字符
print(re.findall("\d*","hhh123sjkdfh34hsbfdh444hsdbfhj88dfd3sb323"))
# ['', '', '', '123', '', '', '', '', '', '', '34', '', '', '', '', '', '',
# '444', '', '', '', '', '', '', '', '88', '', '', '', '3', '', '', '323', '']
# 注意+,*:
# +:至少重复一次,因此一次都没有就什么都不返回
# *:重复零次也可以,因此没有也返回,比如一个数字都没有,也返回空字符
print(re.findall("\d+","hhhhh"))
# []
print(re.findall("\d+","hhhhh123ggg"))
# ['123']
print(re.findall("\d*","hhhhh"))
# ['', '', '', '', '', '']
print(re.findall("\d*","hhhhh123ggg"))
# ['', '', '', '', '', '123', '', '', '', '']
# 一般情况下我们正则匹配这个东西肯定是有的,
# 有的概念:也就是至少有一次才叫有
# 所以无论是+还是*号用到的都是1到无穷次的区域
# 所以效果数据是一致的。
# 可能还不明白,接下来的这个更清晰,很明显最下面,如果用*,后面没数字也可以匹配,乘法算式的提取就没有意义
# 为了防止半边表达式必须严格控制
# 如下为正确的匹配方法
print(re.findall("-?\d+\.?\d*\*\d+\.?\d*","*45-3*4+3*9+5.5*12-3.3*"))
# ['-3*4', '3*9', '5.5*12']
# 如下为错误的匹配方法
print(re.findall("-?\d*\.?\d*\*\d*\.?\d*","*45-3*4+3*9+5.5*12-3.3*"))
# ['*45', '-3*4', '3*9', '5.5*12', '3.3*']
# ?将包涵和不包含的都搞出来,以下就是输出负数和正数
# 所以可以用来包涵正负数,包涵整数小数,一般用在运算符后面
# 一个完整的表达式在运算符左右肯定有数,没有的不能高进来
# 大总结
# 正数和负数的区别是有没有负号,所以"-?"
# 整数和小数的区别是有没有点以及点后面的几个数字:".?\d+"
# 如果只是符号,就不需要转义,比如负号:"-"
# 如果需要运算功能,就必须转义,比如减号,乘号:"\*"
print(re.findall("-?\d+","-1-2-333+5*6/7"))
# 匹配字符串
# 大小写加数字
print(re.findall("\w+","helloiamshone1688hahhhadsshjdvfsbdsd79239823"))
# ['helloiamshone1688hahhhadsshjdvfsbdsd79239823']
print(re.findall("[A-Z]+","helloiamSUNshone1688HUEWRIssa777"))
# ['SUN', 'HUEWRI']
print(re.findall("[a-z]+","helloiamSUNshone1688HUEWRIssa777"))
# ['helloiam', 'shone', 'ssa']
print(re.findall("[0-9]+","helloiamSUNshone1688HUEWRIssa777"))
# ['1688', '777']
print(re.findall("[A-Za-z]+","helloiamSUNshone1688HUEWRIssa777"))
# ['helloiamSUNshone', 'HUEWRIssa']
# 提取乘法\
print("乘法",re.findall("-?\d+\.?d*\*\d+\.?d*","*45-3*4+3*9+5.5*12-3.3*-5+2+3-8+66/6-33/3+88/8"))
# 乘法 ['-3*4', '3*9', '5*12']
# 提取除法
print("除法",re.findall("-?\d+\.?d*\/\d+\.?d*","*45-3*4+3*9+5.5*12-3.3*-5+2+3-8+66/6-33/3+88/8"))
# 除法 ['66/6', '-33/3', '88/8']
# 提取加法
print("加法",re.findall("-?\d+\.?d*\+\d+\.?d*","*45-3*4+3*9+5.5*12-3.3*-5+2+3-8+66/6-33/3+88/8"))
# 加法 ['4+3', '9+5.', '-5+2', '-8+66', '3+88']
# 提取减法
print("减法",re.findall("-?\d+\.?d*\-\d+\.?d*","*45-3*4+3*9+5.5*12-3.3*-5+2+3-8+66/6-33/3+88/8"))
# 减法 ['45-3', '3-8', '6-33']
print(re.findall("p..h","python pggggggh"))
print(re.findall("a[bd]c","pythonabcxxxxadc222adc")) #或者
print(re.findall("188\d{8}","python18867781029\hhhhhhhesdvfhjdfgvsjh32942393993243493299418"))
# . 通配符
# * [0,++]
# + [0,+]
# ?[0,1]
# {n,m}自定义
# [] 字符集,里面*,+等元字符都是普通符号,-,^,\例外
# [0-9]
# [a-z]
# [A-Z]
# [^\d] 取反
print(re.findall("-?\d*\.?\d*\*\d*\.?\d*","-3*4+3*9+5.5*12"))
print(re.findall(r"c\\l","abc\l"))
# ['c\\l']
# split
s = '123.33sdhf3424.34fdg323.324hh333hh123yyyy1.88yydjsd3.88hello'
x = re.findall("\d+\.?\d+",s)
print(x)
print(sum([float(i) for i in x]))
# 有一个数据拿一个数据,所以不会多余
s = '123.33sdhf3424.34fdg323.324hh333hh123yyyy1.88yydjsd3.88hello'
y = re.split("[a-z]+",s)
print(y)
# print(sum([float(i) for i in y]))
# 由于上面这个方法二分割返回的最后是一个空格,无法相加
# 找到进行分割
print(re.split("\d+","fhak2232ahfvbf44422cbcds44",2))
# ['fhak', 'ahfvbf', 'cbcds44']
print(re.split("[a-z]+","fhak2232ahfvbf44422cbcds44",3))
# ['', '2232', '44422', '44']
print(re.findall(r"\w+\\articles\\\d{4}",r"ada\articles\3714"))
# ['ada\\articles\\3714']
import re #导入re模块(正则表达式)
calc2 = "1-2*((60-30+(-40/52)*(9-2*5/3+7/3*-99/4*2998+10*568/14))-(-4*3)/(16-3*2))"
calc1 = "1-2*((60-30+(-40/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))"
calc = "1-2*((60*30+(-40/5)*(9*2*5/3+7/3*99/4*2998+10*568/14))-(-4*32)/(16-3*2))"
def Format(Str):
"格式化运算式"
Str = Str.replace("+-","-")
Str = Str.replace("-+", "-")
Str = Str.replace("++", "+")
Str = Str.replace("--", "+")
return Str
def SumCut(Str):
while re.search("-?[0-9]+\.?[0-9]*[-+][0-9]+\.?[0-9]*",Str):
newRes= re.search("-?[0-9]+\.?[0-9]*[-+][0-9]+\.?[0-9]*",Str)
WhatIs = newRes.group()
if(WhatIs.find("-") > 0):
l = WhatIs.split("-")
Str = Str.replace(WhatIs,str(float(l[0])-float(l[1])))
elif(WhatIs.find("+") > 0):
l = WhatIs.split("+")
Str = Str.replace(WhatIs,str(float(l[0])+float(l[1])))
return Str.replace("(","").replace(")","")
def MulDiv(Str):
while re.search("-?[0-9]+\.?[0-9]*[/*].?[0-9]+\.?[0-9]*",Str):
newRes= re.search("-?[0-9]+\.?[0-9]*[/*].?[0-9]+\.?[0-9]*",Str)
WhatIs = newRes.group()
if(WhatIs.find("/") > 0):
l = WhatIs.split("/")
Str = Str.replace(WhatIs,str(float(l[0])/float(l[1])))
elif(WhatIs.find("*") > 0):
l = WhatIs.split("*")
if(float(l[0])<0 and float(l[1])<0):
Str = Str.replace(WhatIs, "+"+str(float(l[0]) * float(l[1])))
else:
Str = Str.replace(WhatIs,str(float(l[0])*float(l[1])))
return Format(Str)
while re.search("\([^()]+\)",calc):
res = re.search("\([^()]+\)", calc)
resTwo = MulDiv(Format(res.group()))
resTwo = SumCut(resTwo)
calc = calc.replace(res.group(),resTwo)
else:
resTwo = MulDiv(calc)
resTwo = SumCut(resTwo)
calc = resTwo
print(calc)