1 #!/usr/bin/env python3 2 import re 3 def find1(ins): 4 """ 5 查找最里面的()并计算,然后再重复查找()不断的计算,直到没有() 6 :param ins:所需要运算的符号 7 :return: 返回一个不带括号的运算 8 """ 9 if not re.search('\)+',ins) :#判断这个字符串中是否还有括号,没有则退出 10 return ins 11 t=ins.find(')') 12 # print(t) 13 # print(ins[t]) 14 flag=True 15 num=t 16 while flag: 17 if ins[num] == '(': 18 flag=False 19 num1=num 20 continue 21 num = num - 1 22 m=ins[num1:t+1] #切片字符串 23 # print(m) 24 ret=eval(m) 25 # print(ret) 26 ins2=ins.replace(m,str(ret))#将计算的结果代替() 27 ret3=find1(ins2)#再重新找下一下() 28 return ret3 29 def check(args): 30 if re.search(r'[^\+\-*/()0-9\.]',args):#判断是否有数字+-*/().以后的非法字符 31 return False 32 if re.search('\(\)|\)\(',args): #判断() )(则出错 33 return False 34 if re.search(r'(\([\*\/])|([\+\-*/]\))',args):#判断(*或(/ + - * /)则出错 35 return False 36 if re.search(r'\.[\+\-*/()\.]',args): 37 return False 38 t1=re.findall(r'\(',args) 39 t2=re.findall(r'\)',args) 40 if len(t1) != len(t2): 41 return False 42 return True 43 ins1=input("please input str:") 44 check_str=check(ins1) 45 if check_str: 46 ret100=find1(ins1) 47 sum100 = eval(ret100) 48 print(sum100) 49 else: 50 print("输入表达式出错")
1 import re 2 print(re.findall("abc","asdas abc")) 3 print(re.findall("abc\\b","asdasabc**")) 4 print(re.findall(r'I\b','I MISS IOU')) 5 print(re.findall("www.(baidu|laonanhai).com","sdad www.baidu.com")) 6 print(re.findall("www.(?:baidu|laonanhai).com","sdad www.baidu.com"))#?:去掉优先捕获 7 """ 8 运行结果: 9 C:\Python27\python.exe H:/python17/day08/re1.py 10 ['abc'] 11 ['abc'] 12 ['I'] 13 ['baidu'] 14 ['www.baidu.com'] 15 """
1 str1='one13two2three3four4' 2 regex=re.compile(r'\d+')#以数字分割 3 print(regex.split(str1)) 4 print(regex.findall(str1)) 5 t=re.search(r'\d+',str1) 6 # print(t.group()) 7 t2=re.match(r'\d+',str1) 8 print(t2) 9 """ 10 ['one', 'two', 'three', 'four', ''] 11 ['13', '2', '3', '4'] 12 None 13 """
import re t=re.findall("com","COM",re.I)#re.I模式忽略大小写 print(t) t1=re.findall(".","abc\nde") print(t1) t2=re.findall(".","abc\nde",re.S) #re.S匹配特殊字符 print(t2) """ ['COM'] ['a', 'b', 'c', 'd', 'e'] ['a', 'b', 'c', '\n', 'd', 'e'] """
1 a="123abc456" 2 t=re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(0) 3 print(t) 4 t1=re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(1) 5 print(t1) 6 t2=re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(2) 7 print(t2) 8 t3=re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(3) 9 print(t3) 10 #========= 11 """ 12 123abc456 13 123 14 abc 15 456 16 """
1 print(re.sub("g.t","have",'I get A,I got B,I gut C')) 2 print(re.sub("g.t","have",'I get A,I got B,I gut C',2)) 3 print(re.subn("g.t","have",'I get A,I got B,I gut C')) #可以计数到底被替换了多少次 4 5 text="JGood is a handsome boy,he is cool,clever,and so " 6 regex = re.compile(r'\w*oo\w*')#查找所有包含'oo'的单词 7 print(regex.findall(text)) 8 """ 9 I have A,I have B,I have C 10 I have A,I have B,I gut C 11 ('I have A,I have B,I have C', 3) 12 ['JGood', 'cool'] 13 """
元字符 说明 . 代表任意字符 | 逻辑或操作符 [ ] 匹配内部的任一字符或子表达式 [^] 对字符集和取非 - 定义一个区间 \ 对下一字符取非(通常是普通变特殊,特殊变普通) * 匹配前面的字符或者子表达式0次或多次 *? 惰性匹配上一个 + 匹配前一个字符或子表达式一次或多次 +? 惰性匹配上一个 ? 匹配前一个字符或子表达式0次或1次重复 {n} 匹配前一个字符或子表达式 {m,n} 匹配前一个字符或子表达式至少m次至多n次 {n,} 匹配前一个字符或者子表达式至少n次 {n,}? 前一个的惰性匹配 ^ 匹配字符串的开头 \A 匹配字符串开头 $ 匹配字符串结束 [\b] 退格字符 \c 匹配一个控制字符 \d 匹配任意数字 \D 匹配数字以外的字符 \t 匹配制表符 \w 匹配任意数字字母下划线 \W 不匹配数字字母下划线