正则表达式: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 #\n \t都是空,都可以被\s匹配 17 print(re.findall('\s','hello \n egon \t 123')) #[' ', '\n', ' ', ' ', '\t', ' '] 18 19 #\n与\t 20 print(re.findall(r'\n','hello egon \n123')) #['\n'] 21 print(re.findall(r'\t','hello egon\t123')) #['\n'] 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与\Z 28 print(re.findall('\Ahe','hello egon 123')) #['he'],\A==>^ 29 print(re.findall('123\Z','hello egon 123')) #['he'],\Z==>$ 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\nb')) #[] 40 print(re.findall('a.b','a\nb',re.S)) #['a\nb'] 41 print(re.findall('a.b','a\nb',re.DOTALL)) #['a\nb']同上一条意思一样 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','a\c')) #对于正则来说a\\c确实可以匹配到a\c,但是在python解释器读取a\\c时,会发生转义,然后交给re去执行,所以抛出异常 78 print(re.findall(r'a\\c','a\c')) #r代表告诉解释器使用rawstring,即原生字符串,把我们正则内的所有符号都当普通字符处理,不要转义 79 print(re.findall('a\\\\c','a\c')) #同上面的意思一样,和上面的结果一样都是['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'\byuan','yuanasas yuan saddsadsyuan')) print(re.findall(r'yuan\b','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'\5\2\3\4\1','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('Hello\s\d\d\d\s\d{3}\s\w{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('^Hello\s(\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('<a\shref=.*?<b\stitle="(.*?)".*?b>',content) 117 # print(res.group(1)) 118 119 120 #re.findall:找到符合条件的所有结果 121 # res=re.findall('<a\shref=.*?<b\stitle="(.*?)".*?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'\1\4\3\2\5',content) 141 # content=re.sub('(\d+)(\s)(\d+)',r'\3\2\1',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('\033[32;1m提取计算的表达式为%s:\033[0m' % variate) 70 ret = computer(variate) 71 ret = ret[1:len(ret)-1] 72 print('\033[32;1m表达式的结果为:%.2f\033[0m' % float(ret)) 73 before,nothing,after = computerc_pattern.split(expression,1) 74 new_str = '%s%s%s' % (before,ret,after) 75 print('\033[32;1mAfter:计算后的表达式\033[0m'.center(100,'*')) 76 print('\033[31;1m>>>>>>>>>:%s\033[0m' % new_str) 77 expression = new_str 78 return operations(expression) 79 def menu(): 80 while True: 81 user_input = input('\033[31;1m请输入你的表达式:\033[0m') 82 user_input = re.sub('\s*','',user_input) 83 if user_input.lower() == 'q': 84 sys.exit() 85 if len(user_input) == 0 : 86 continue 87 if re.search('[^0-9\.\+\-\*\/\(\)]',user_input): 88 print('输的表达式错误,请重新输入!') 89 continue 90 else: 91 print('\033[32;1m开始计算表达式%s的值:\033[0m' % user_input) 92 result = operations(user_input) 93 print('\033[32;1m最后计算结果为:%.2f(精确小数后两位)\033[0m' % float(result))
1 from random import randint , choice 2 from string import ascii_lowercase 3 from sys import maxunicode 4 from time import ctime 5 # import logging 6 7 # def Logger(): 8 # Logger = logging.getLogger() 9 # file_print = logging.FileHandler('random_file') 10 # stream_print = logging.StreamHandler() 11 # Logger.addHandler(file_print) 12 # Logger.addHandler(stream_print) 13 # return Logger 14 doms = ('com','edu','net','org','gov') 15 for i in range(randint(0,10)): 16 dtint = randint(0,maxunicode-1) 17 dtstr = ctime(dtint) 18 shorter = randint(4,7) 19 em = ' ' 20 for j in range(shorter): 21 em += choice(ascii_lowercase) 22 23 longer = randint(shorter,12) 24 dn = '' 25 for j in range(longer): 26 dn += choice(ascii_lowercase) 27 # res = Logger() 28 # res.warning('%s::%s@%s.%s::%d-%d-%d' % (dtstr,em,dn,choice(doms),dtint,shorter,longer)) 29 print('%s::%s@%s.%s::%d-%d-%d' % (dtstr,em,dn,choice(doms),dtint,shorter,longer)) 30 31 >>>>>>>>> 32 Sun Jan 4 05:07:10 1970:: pssgd@abbcuzezete.org::248830-5-11 33 Wed Jan 7 12:40:35 1970:: hquvpjq@ewvbngel.net::535235-7-8 34 Mon Jan 5 22:25:58 1970:: ncpvtkm@elrujelzc.org::397558-7-9 35 Wed Jan 7 19:27:31 1970:: xvkfjka@aqrcwbf.com::559651-7-7 36 Sun Jan 11 11:59:51 1970:: mykrmx@ltqbhkkpdtzi.com::878391-6-12 37 Fri Jan 2 00:32:17 1970:: mhujbnz@juxofli.gov::59537-7-7 38 Sat Jan 3 09:55:38 1970:: pyvnri@bxbncni.org::179738-6-7 39 Sat Jan 10 14:52:24 1970:: jjsnb@ujcquqzamzj.edu::802344-5-11