正则表达式:re模块
1.正则表达式的作用是:实现对字符串的模糊查询。
2.正则表达式使用的元字符(特殊符号和字符):
http://blog.csdn.net/yufenghyc/article/details/51078107
1 # =================================匹配模式================================= 2 #一对一的匹配 3 # 'hello'.replace(old,new) 4 # 'hello'.find('pattern') 5 6 #正则匹配 7 import re 8 #w与W 9 print(re.findall('w','hello egon 123')) #['h', 'e', 'l', 'l', 'o', 'e', 'g', 'o', 'n', '1', '2', '3'] 10 print(re.findall('W','hello egon 123')) #[' ', ' '] 11 12 #s与S 13 print(re.findall('s','hello egon 123')) #[' ', ' ', ' ', ' '] 14 print(re.findall('S','hello egon 123')) #['h', 'e', 'l', 'l', 'o', 'e', 'g', 'o', 'n', '1', '2', '3'] 15 16 # 都是空,都可以被s匹配 17 print(re.findall('s','hello egon 123')) #[' ', ' ', ' ', ' ', ' ', ' '] 18 19 # 与 20 print(re.findall(r' ','hello egon 123')) #[' '] 21 print(re.findall(r' ','hello egon 123')) #[' '] 22 23 #d与D 24 print(re.findall('d','hello egon 123')) #['1', '2', '3'] 25 print(re.findall('D','hello egon 123')) #['h', 'e', 'l', 'l', 'o', ' ', 'e', 'g', 'o', 'n', ' '] 26 27 #A与 28 print(re.findall('Ahe','hello egon 123')) #['he'],A==>^ 29 print(re.findall('123','hello egon 123')) #['he'],==>$ 30 31 #^与$ 32 print(re.findall('^h','hello egon 123')) #['h'] 33 print(re.findall('3$','hello egon 123')) #['3'] 34 35 # 重复匹配:| . | * | ? | .* | .*? | + | {n,m} | 36 #. 37 print(re.findall('a.b','a1b')) #['a1b'] 38 print(re.findall('a.b','a1b a*b a b aaab')) #['a1b', 'a*b', 'a b', 'aab'] 39 print(re.findall('a.b','a b')) #[] 40 print(re.findall('a.b','a b',re.S)) #['a b'] 41 print(re.findall('a.b','a b',re.DOTALL)) #['a b']同上一条意思一样 42 43 #* 44 print(re.findall('ab*','bbbbbbb')) #[] 45 print(re.findall('ab*','a')) #['a'] 46 print(re.findall('ab*','abbbb')) #['abbbb'] 47 48 #? 49 print(re.findall('ab?','a')) #['a'] 50 print(re.findall('ab?','abbb')) #['ab'] 51 #匹配所有包含小数在内的数字 52 print(re.findall('d+.?d*',"asdfasdf123as1.13dfa12adsf1asdf3")) #['123', '1.13', '12', '1', '3'] 53 54 #.*默认为贪婪匹配 55 print(re.findall('a.*b','a1b22222222b')) #['a1b22222222b'] 56 57 #.*?为非贪婪匹配:推荐使用 58 print(re.findall('a.*?b','a1b22222222b')) #['a1b'] 59 60 #+ 61 print(re.findall('ab+','a')) #[] 62 print(re.findall('ab+','abbb')) #['abbb'] 63 64 #{n,m} 65 print(re.findall('ab{2}','abbb')) #['abb'] 66 print(re.findall('ab{2,4}','abbb')) #['abb'] 67 print(re.findall('ab{1,}','abbb')) #'ab{1,}' ===> 'ab+' 68 print(re.findall('ab{0,}','abbb')) #'ab{0,}' ===> 'ab*' 69 70 #[] 71 print(re.findall('a[1*-]b','a1b a*b a-b')) #[]内的都为普通字符了,且如果-没有被转意的话,应该放到[]的开头或结尾 72 print(re.findall('a[^1*-]b','a1b a*b a-b a=b')) #[]内的^代表的意思是取反,所以结果为['a=b'] 73 print(re.findall('a[0-9]b','a1b a*b a-b a=b')) #[]内的^代表的意思是取反,所以结果为['a=b'] 74 print(re.findall('a[a-z]b','a1b a*b a-b a=b aeb')) #[]内的^代表的意思是取反,所以结果为['a=b'] 75 print(re.findall('a[a-zA-Z]b','a1b a*b a-b a=b aeb aEb')) #[]内的^代表的意思是取反,所以结果为['a=b'] 76 77 ## print(re.findall('a\c','ac')) #对于正则来说a\c确实可以匹配到ac,但是在python解释器读取a\c时,会发生转义,然后交给re去执行,所以抛出异常 78 print(re.findall(r'a\c','ac')) #r代表告诉解释器使用rawstring,即原生字符串,把我们正则内的所有符号都当普通字符处理,不要转义 79 print(re.findall('a\\c','ac')) #同上面的意思一样,和上面的结果一样都是['a\c'] 80 81 #():分组 82 print(re.findall('ab+','ababab123')) #['ab', 'ab', 'ab'] 83 print(re.findall('(ab)+123','ababab123')) #['ab'],匹配到末尾的ab123中的ab 84 print(re.findall('(?:ab)+123','ababab123')) #findall的结果不是匹配的全部内容,而是组内的内容,?:可以让结果为匹配的全部内容 85 86 #| 87 print(re.findall('compan(?:y|ies)','Too many companies have gone bankrupt, and the next one is my company'))
import re print(re.findall('.','abc')) print(re.findall('.*','abc')) print(re.findall('.+','abc')) print(re.findall('.?','abc')) print(re.findall('.{2}','abcd')) print(re.findall('.{1,3}','abcd')) print(re.findall('^abc','abcdcadasd')) print(re.findall('^abc.+','abcdcadasd')) print(re.findall('asd$','abcdcadasd')) print(re.findall('.+asd$','abcdcadasd')) print(re.findall(r'yuan','yuanasas yuan saddsadsyuan')) print(re.findall(r'yuan','yuanasas sasayuan saddsadsyuan')) print(re.findall(r'Byuan','yuanasas sasayuansad yuan dasdasyuan')) print(re.findall('Byuan','yu1anssyuan adsdyuandasdyuanyuan yuan1yuan')) print(re.findall('Byuan','yuanssyuan adsdyuandasdyuan')) print(re.findall('y[abcd]an','yaanybanycanydn')) print(re.findall('ab|cd','abcd')) #不能用[],因为[]只使用与单个的字符的or,多个字符要用| print(re.findall('[0-9]+','12123dsad2233dasd2')) print(re.findall('[A-z]+','dadas11daADDASDd2dsadasddasdasds11')) print(re.findall('[^A-z]+','asdasd12123123dd313121d2122121'))#[]里面的^取非 print(re.findall('[^aeiou]+','asdascas12221oisaouidoasodji'))#非元音字符 res = re.search('d+(.?d*)','1.123 121.21 0.122') res1 =re.compile('d{4}-d{8}') print(res1.findall('02112-1212123dsada0791-888888881212dsadas')) res2 = re.compile('w+@w+.w+') print(res2.findall('576561638@qq.com')) print(res2.findall('576561638@163.com')) print(re.findall('.+(d+-d+-d+)','Tue Jan 13 04:26:57 1970:: unta@sljrpn.edu::1024017-4-6')) #贪婪匹配 print(re.findall('.+?(d+-d+-d+)','Tue Jan 13 04:26:57 1970:: unta@sljrpn.edu::1024017-4-6')) #非贪婪匹配 >>>>>>>>> ['a', 'b', 'c'] ['abc', ''] ['abc'] ['a', 'b', 'c', ''] ['ab', 'cd'] ['abc', 'd'] ['abc'] ['abcdcadasd'] ['asd'] ['abcdcadasd'] ['yuan', 'yuan'] ['yuan', 'yuan'] ['yuan', 'yuan'] ['yuan', 'yuan', 'yuan', 'yuan', 'yuan'] ['yuan', 'yuan', 'yuan'] ['yaan', 'yban', 'ycan'] ['ab', 'cd'] ['12123', '2233', '2'] ['dadas', 'daADDASDd', 'dsadasddasdasds'] ['12123123', '313121', '2122121'] ['sd', 'sc', 's12221', 's', 'd', 's', 'dj'] ['0791-88888888'] ['576561638@qq.com'] ['576561638@163.com'] ['7-4-6'] ['1024017-4-6']
3.re模块的方法:
# ===========================re模块提供的方法介绍=========================== import re #1 print(re.findall('e','alex make love') ) #['e', 'e', 'e'],返回所有满足匹配条件的结果,放在列表里 #2 print(re.search('e','alex make love').group()) #e,只到找到第一个匹配然后返回一个包含匹配信息的对象,该对象可以通过调用group()方法得到匹配的字符串,如果字符串没有匹配,则返回None。 #3 print(re.match('e','alex make love')) #None,同search,不过在字符串开始处进行匹配,完全可以用search+^代替match #4 print(re.split('[ab]','abcd')) #['', '', 'cd'],先按'a'分割得到''和'bcd',再对''和'bcd'分别按'b'分割 #5 print('===>',re.sub('a','A','alex make love')) #===> Alex mAke love,不指定n,默认替换所有 print('===>',re.sub('a','A','alex make love',1)) #===> Alex make love print('===>',re.sub('a','A','alex make love',2)) #===> Alex mAke love print('===>',re.sub('^(w+)(.*?s)(w+)(.*?s)(w+)(.*?)$',r'52341','alex make love')) #===> love make alex print('===>',re.subn('a','A','alex make love')) #===> ('Alex mAke love', 2),结果带有总共替换的个数 #6 obj=re.compile('d{2}') print(obj.search('abc123eeee').group()) #12 print(obj.findall('abc123eeee')) #['12'],重用了obj
import re print(re.findall("<(?P<tag_name>w+)>w+</(?P=tag_name)>","<h1>hello</h1>")) #['h1'] print(re.search("<(?P<tag_name>w+)>w+</(?P=tag_name)>","<h1>hello</h1>").group()) #<h1>hello</h1> print(re.search("<(?P<tag_name>w+)>w+</(?P=tag_name)>","<h1>hello</h1>").groupdict()) #<h1>hello</h1> print(re.search(r"<(w+)>w+</(w+)>","<h1>hello</h1>").group()) print(re.search(r"<(w+)>w+</1>","<h1>hello</h1>").group()) import re print(re.findall(r'-?d+.?d*',"1-12*(60+(-40.35/5)-(-4*3))")) #找出所有数字['1', '-12', '60', '-40.35', '5', '-4', '3'] #使用|,先匹配的先生效,|左边是匹配小数,而findall最终结果是查看分组,所有即使匹配成功小数也不会存入结果 #而不是小数时,就去匹配(-?d+),匹配到的自然就是,非小数的数,在此处即整数 print(re.findall(r"-?d+.d*|(-?d+)","1-2*(60+(-40.35/5)-(-4*3))")) #找出所有整数['1', '-2', '60', '', '5', '-4', '3']
#为何同样的表达式search与findall却有不同结果: print(re.search('(([+-*/]*d+.?d*)+)',"1-12*(60+(-40.35/5)-(-4*3))").group()) #(-40.35/5) print(re.findall('(([+-*/]*d+.?d*)+)',"1-12*(60+(-40.35/5)-(-4*3))")) #['/5', '*3'] #看这个例子:(d)+相当于(d)(d)(d)(d)...,是一系列分组 print(re.search('(d)+','123').group()) #group的作用是将所有组拼接到一起显示出来 print(re.findall('(d)+','123')) #findall结果是组内的结果,且是最后一个组的结果 search与findall
1 #_*_coding:utf-8_*_ 2 __author__ = 'Linhaifeng' 3 #在线调试工具:tool.oschina.net/regex/# 4 import re 5 6 s=''' 7 http://www.baidu.com 8 egon@oldboyedu.com 9 你好 10 010-3141 11 ''' 12 13 #最常规匹配 14 # content='Hello 123 456 World_This is a Regex Demo' 15 # res=re.match('Hellosdddsd{3}sw{10}.*Demo',content) 16 # print(res) 17 # print(res.group()) 18 # print(res.span()) 19 20 #泛匹配 21 # content='Hello 123 456 World_This is a Regex Demo' 22 # res=re.match('^Hello.*Demo',content) 23 # print(res.group()) 24 25 26 #匹配目标,获得指定数据 27 28 # content='Hello 123 456 World_This is a Regex Demo' 29 # res=re.match('^Hellos(d+)s(d+)s.*Demo',content) 30 # print(res.group()) #取所有匹配的内容 31 # print(res.group(1)) #取匹配的第一个括号内的内容 32 # print(res.group(2)) #去陪陪的第二个括号内的内容 33 34 35 36 #贪婪匹配:.*代表匹配尽可能多的字符 37 # import re 38 # content='Hello 123 456 World_This is a Regex Demo' 39 # 40 # res=re.match('^He.*(d+).*Demo$',content) 41 # print(res.group(1)) #只打印6,因为.*会尽可能多的匹配,然后后面跟至少一个数字 42 43 44 #非贪婪匹配:?匹配尽可能少的字符 45 # import re 46 # content='Hello 123 456 World_This is a Regex Demo' 47 # 48 # res=re.match('^He.*?(d+).*Demo$',content) 49 # print(res.group(1)) #只打印6,因为.*会尽可能多的匹配,然后后面跟至少一个数字 50 51 52 #匹配模式:.不能匹配换行符 53 content='''Hello 123456 World_This 54 is a Regex Demo 55 ''' 56 # res=re.match('He.*?(d+).*?Demo$',content) 57 # print(res) #输出None 58 59 # res=re.match('He.*?(d+).*?Demo$',content,re.S) #re.S让.可以匹配换行符 60 # print(res) 61 # print(res.group(1)) 62 63 64 #转义: 65 66 # content='price is $5.00' 67 # res=re.match('price is $5.00',content) 68 # print(res) 69 # 70 # res=re.match('price is $5.00',content) 71 # print(res) 72 73 74 #总结:尽量精简,详细的如下 75 # 尽量使用泛匹配模式.* 76 # 尽量使用非贪婪模式:.*? 77 # 使用括号得到匹配目标:用group(n)去取得结果 78 # 有换行符就用re.S:修改模式 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 #re.search:会扫描整个字符串,不会从头开始,找到第一个匹配的结果就会返回 95 96 # import re 97 # content='Extra strings Hello 123 456 World_This is a Regex Demo Extra strings' 98 # 99 # res=re.match('Hello.*?(d+).*?Demo',content) 100 # print(res) #输出结果为None 101 102 # 103 # import re 104 # content='Extra strings Hello 123 456 World_This is a Regex Demo Extra strings' 105 # 106 # res=re.search('Hello.*?(d+).*?Demo',content) # 107 # print(res.group(1)) #输出结果为 108 109 110 111 #re.search:只要一个结果,匹配演练, 112 import re 113 content=''' 114 <tbody> 115 <tr id="4766303201494371851675" class="even "><td><div class="hd"><span class="num">1</span><div class="rk "><span class="u-icn u-icn-75"></span></div></div></td><td class="rank"><div class="f-cb"><div class="tt"><a href="/song?id=476630320"><img class="rpic" src="http://p1.music.126.net/Wl7T1LBRhZFg0O26nnR2iQ==/19217264230385030.jpg?param=50y50&quality=100"></a><span data-res-id="476630320" " 116 # res=re.search('<ashref=.*?<bstitle="(.*?)".*?b>',content) 117 # print(res.group(1)) 118 119 120 #re.findall:找到符合条件的所有结果 121 # res=re.findall('<ashref=.*?<bstitle="(.*?)".*?b>',content) 122 # for i in res: 123 # print(i) 124 125 126 127 #re.sub:字符串替换 128 import re 129 content='Extra strings Hello 123 456 World_This is a Regex Demo Extra strings' 130 131 # content=re.sub('d+','',content) 132 # print(content) 133 134 135 #用1取得第一个括号的内容 136 #用法:将123与456换位置 137 # import re 138 # content='Extra strings Hello 123 456 World_This is a Regex Demo Extra strings' 139 # 140 # # content=re.sub('(Extra.*?)(d+)(s)(d+)(.*?strings)',r'14325',content) 141 # content=re.sub('(d+)(s)(d+)',r'321',content) 142 # print(content) 143 144 145 146 147 # import re 148 # content='Extra strings Hello 123 456 World_This is a Regex Demo Extra strings' 149 # 150 # res=re.search('Extra.*?(d+).*strings',content) 151 # print(res.group(1)) 152 153 154 # import requests,re 155 # respone=requests.get('https://book.douban.com/').text 156 157 # print(respone) 158 # print('======'*1000) 159 # print('======'*1000) 160 # print('======'*1000) 161 # print('======'*1000) 162 # res=re.findall('<li.*?cover.*?href="(.*?)".*?title="(.*?)">.*?more-meta.*?author">(.*?)</span.*?year">(.*?)</span.*?publisher">(.*?)</span.*?</li>',respone,re.S) 163 # # res=re.findall('<li.*?cover.*?href="(.*?)".*?more-meta.*?author">(.*?)</span.*?year">(.*?)</span.*?publisher">(.*?)</span>.*?</li>',respone,re.S) 164 # 165 # 166 # for i in res: 167 # print('%s %s %s %s' %(i[0].strip(),i[1].strip(),i[2].strip(),i[3].strip()))
#计算器作业参考:http://www.cnblogs.com/wupeiqi/articles/4949995.html
expression='1-2*((60+2*(-3-40.0/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))'
content=re.search('(([-+*/]*d+.?d*)+)',expression).group() #(-3-40.0/5)
1 import re ,sys 2 '1-2*((60+2*(-3-40.0/5*-4)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))' 3 def Multiply_divide(variate_list): 4 multiply_variate= variate_list[0] 5 pattern = re.compile('d+.*d*[*/]{1}[+-]?d+.*d*') 6 flag = pattern.search(multiply_variate) 7 if not flag: 8 return 9 multiply_variate1 = pattern.search(multiply_variate).group() 10 # print(multiply_variate1) 11 if multiply_variate1.__contains__('*'): 12 n1,n2 = multiply_variate1.split('*') 13 value = float(n1) * float(n2) 14 else: 15 n1,n2 = multiply_variate1.split('/') 16 value = float(n1) / float(n2) 17 before,after = pattern.split(multiply_variate,1) 18 new_variate = "%s%s%s" % (before,value,after) 19 variate_list[0] = new_variate 20 Multiply_divide(variate_list) 21 def add_substract(variate_list): 22 add_variate = variate_list[0] 23 while True: 24 if add_variate.__contains__('--') or add_variate.__contains__('+-') or add_variate.__contains__('++') or add_variate.__contains__('-+'): 25 add_variate =add_variate.replace('--','+') 26 add_variate =add_variate.replace('+-','-') 27 add_variate =add_variate.replace('++','+') 28 add_variate =add_variate.replace('-+','-') 29 else:break 30 pattern = re.compile('[+-]?d+.?d*[+|-]{1}d+.?d*') 31 flag = pattern.search(add_variate) 32 if not flag: 33 return 34 add_variate1 = pattern.search(add_variate).group() 35 if add_variate1.startswith('-'): 36 variate_list[1] = 2 37 add_variate1 = add_variate1.replace('-','&') 38 add_variate1 = add_variate1.replace('+','-') 39 add_variate1 = add_variate1.replace('&','+') 40 add_variate1 = add_variate1[1:len(add_variate1)-1] 41 if add_variate1.__contains__('-'): 42 n1,n2 = add_variate1.split('-') 43 value = float(n1) - float(n2) 44 if add_variate1.__contains__('+'): 45 n1,n2 = add_variate1.split('+') 46 value = float(n1) + float(n2) 47 if variate_list[1] == 2: 48 value = value * -1 49 variate_list[1] = 1 50 else:value = value 51 before,after = pattern.split(add_variate,1) 52 new_str = '%s%s%s' % (before,value,after) 53 variate_list[0] = new_str 54 add_substract(variate_list) 55 def computer(arg): 56 variate_list = [arg,0] 57 # print(variate_list) 58 Multiply_divide(variate_list) 59 add_substract(variate_list) 60 # print(variate_list) 61 return variate_list[0] 62 def operations(arg): 63 expression = arg 64 computerc_pattern = re.compile('(([+-*/]*d+.*d*){2,})') 65 if not computerc_pattern.search(expression): 66 final = computer(expression) 67 return final 68 variate = computerc_pattern.search(expression).group() 69 print('