什么是正则?
正则就是用一些具有特殊含义的符号组合到一起(称之为正则表达式)来描述字符或字符串的方法,或者
正则就是用来描述事物的规则,在(python)中,他内嵌在python中,并通过re模块实现,正则表达式模式被编译成一系列的字节码,
然后用c编写的匹配引擎执行
常用匹配字符:
w匹配数字字母下划线
W匹配非数字字母下划线
s匹配任意空白字符如 f
S匹配任意非空字符
d匹配任意数字
D匹配任意非数字
匹配一个换行符
匹配一个制表符
^匹配字符串的开头
$匹配字符串的末尾
.匹配任意字符除了换行符,指定re.DOTALL时,可匹配换行符
[...]用来匹配中括号内一组字符中的一个
[^...]用来匹配不在中括号中的字符
*匹配零个或多个的表达式
+匹配一个或多个的表达式
?匹配零个或一个
{n}精确匹配n个前面表达式
{n,m}匹配n到m次由前面的正则表达式定义的片段
a|b匹配a或b
()匹配括号内的表达式
1 re规则 2 # =================================匹配模式================================= 3 #一对一的匹配 4 # 'hello'.replace(old,new) 5 # 'hello'.find('pattern') 6 7 #正则匹配 8 import re 9 #w与W 10 print(re.findall('w','hello egon 123')) #['h', 'e', 'l', 'l', 'o', 'e', 'g', 'o', 'n', '1', '2', '3'] 11 print(re.findall('W','hello egon 123')) #[' ', ' '] 12 13 #s与S 14 print(re.findall('s','hello egon 123')) #[' ', ' ', ' ', ' '] 15 print(re.findall('S','hello egon 123')) #['h', 'e', 'l', 'l', 'o', 'e', 'g', 'o', 'n', '1', '2', '3'] 16 17 # 都是空,都可以被s匹配 18 print(re.findall('s','hello egon 123')) #[' ', ' ', ' ', ' ', ' ', ' '] 19 20 # 与 21 print(re.findall(r' ','hello egon 123')) #[' '] 22 print(re.findall(r' ','hello egon 123')) #[' '] 23 24 #d与D 25 print(re.findall('d','hello egon 123')) #['1', '2', '3'] 26 print(re.findall('D','hello egon 123')) #['h', 'e', 'l', 'l', 'o', ' ', 'e', 'g', 'o', 'n', ' '] 27 28 #A与 29 print(re.findall('Ahe','hello egon 123')) #['he'],A==>^ 30 print(re.findall('123','hello egon 123')) #['he'],==>$ 31 32 #^与$ 33 print(re.findall('^h','hello egon 123')) #['h'] 34 print(re.findall('3$','hello egon 123')) #['3'] 35 36 # 重复匹配:| . | * | ? | .* | .*? | + | {n,m} | 37 #. 38 print(re.findall('a.b','a1b')) #['a1b'] 39 print(re.findall('a.b','a1b a*b a b aaab')) #['a1b', 'a*b', 'a b', 'aab'] 40 print(re.findall('a.b','a b')) #[] 41 print(re.findall('a.b','a b',re.S)) #['a b'] 42 print(re.findall('a.b','a b',re.DOTALL)) #['a b']同上一条意思一样 43 44 #* 45 print(re.findall('ab*','bbbbbbb')) #[] 46 print(re.findall('ab*','a')) #['a'] 47 print(re.findall('ab*','abbbb')) #['abbbb'] 48 49 #? 50 print(re.findall('ab?','a')) #['a'] 51 print(re.findall('ab?','abbb')) #['ab'] 52 #匹配所有包含小数在内的数字 53 print(re.findall('d+.?d*',"asdfasdf123as1.13dfa12adsf1asdf3")) #['123', '1.13', '12', '1', '3'] 54 55 #.*默认为贪婪匹配 56 print(re.findall('a.*b','a1b22222222b')) #['a1b22222222b'] 57 58 #.*?为非贪婪匹配:推荐使用 59 print(re.findall('a.*?b','a1b22222222b')) #['a1b'] 60 61 #+ 62 print(re.findall('ab+','a')) #[] 63 print(re.findall('ab+','abbb')) #['abbb'] 64 65 #{n,m} 66 print(re.findall('ab{2}','abbb')) #['abb'] 67 print(re.findall('ab{2,4}','abbb')) #['abb'] 68 print(re.findall('ab{1,}','abbb')) #'ab{1,}' ===> 'ab+' 69 print(re.findall('ab{0,}','abbb')) #'ab{0,}' ===> 'ab*' 70 71 #[] 72 print(re.findall('a[1*-]b','a1b a*b a-b')) #[]内的都为普通字符了,且如果-没有被转意的话,应该放到[]的开头或结尾 73 print(re.findall('a[^1*-]b','a1b a*b a-b a=b')) #[]内的^代表的意思是取反,所以结果为['a=b'] 74 print(re.findall('a[0-9]b','a1b a*b a-b a=b')) #[]内的^代表的意思是取反,所以结果为['a=b'] 75 print(re.findall('a[a-z]b','a1b a*b a-b a=b aeb')) #[]内的^代表的意思是取反,所以结果为['a=b'] 76 print(re.findall('a[a-zA-Z]b','a1b a*b a-b a=b aeb aEb')) #[]内的^代表的意思是取反,所以结果为['a=b'] 77 78 ## print(re.findall('a\c','ac')) #对于正则来说a\c确实可以匹配到ac,但是在python解释器读取a\c时,会发生转义,然后交给re去执行,所以抛出异常 79 print(re.findall(r'a\c','ac')) #r代表告诉解释器使用rawstring,即原生字符串,把我们正则内的所有符号都当普通字符处理,不要转义 80 print(re.findall('a\\c','ac')) #同上面的意思一样,和上面的结果一样都是['a\c'] 81 82 #():分组 83 print(re.findall('ab+','ababab123')) #['ab', 'ab', 'ab'] 84 print(re.findall('(ab)+123','ababab123')) #['ab'],匹配到末尾的ab123中的ab 85 print(re.findall('(?:ab)+123','ababab123')) #findall的结果不是匹配的全部内容,而是组内的内容,?:可以让结果为匹配的全部内容 86 print(re.findall('href="(.*?)"','<a href="http://www.baidu.com">点击</a>'))#['http://www.baidu.com'] 87 print(re.findall('href="(?:.*?)"','<a href="http://www.baidu.com">点击</a>'))#['href="http://www.baidu.com"'] 88 89 #| 90 print(re.findall('compan(?:y|ies)','Too many companies have gone bankrupt, and the next one is my company'))
re模块方法
1 # ===========================re模块提供的方法介绍=========================== 2 import re 3 #1 4 print(re.findall('e','alex make love') ) #['e', 'e', 'e'],返回所有满足匹配条件的结果,放在列表里 5 #2 6 print(re.search('e','alex make love').group()) #e,只到找到第一个匹配然后返回一个包含匹配信息的对象,该对象可以通过调用group()方法得到匹配的字符串,如果字符串没有匹配,则返回None。 7 8 #3 9 print(re.match('e','alex make love')) #None,同search,不过在字符串开始处进行匹配,完全可以用search+^代替match 10 11 #4 12 print(re.split('[ab]','abcd')) #['', '', 'cd'],先按'a'分割得到''和'bcd',再对''和'bcd'分别按'b'分割 13 14 #5 15 print('===>',re.sub('a','A','alex make love')) #===> Alex mAke love,不指定n,默认替换所有 16 print('===>',re.sub('a','A','alex make love',1)) #===> Alex make love 17 print('===>',re.sub('a','A','alex make love',2)) #===> Alex mAke love 18 print('===>',re.sub('^(w+)(.*?s)(w+)(.*?s)(w+)(.*?)$',r'52341','alex make love')) #===> love make alex 19 20 print('===>',re.subn('a','A','alex make love')) #===> ('Alex mAke love', 2),结果带有总共替换的个数 21 22 23 #6 24 obj=re.compile('d{2}') 25 26 print(obj.search('abc123eeee').group()) #12 27 print(obj.findall('abc123eeee')) #['12'],重用了obj
补充二:
1 复制代码 2 #补充二 3 import re 4 5 #使用|,先匹配的先生效,|左边是匹配小数,而findall最终结果是查看分组,所有即使匹配成功小数也不会存入结果 6 #而不是小数时,就去匹配(-?d+),匹配到的自然就是,非小数的数,在此处即整数 7 # 8 print(re.findall(r"-?d+.d*|(-?d+)","1-2*(60+(-40.35/5)-(-4*3))")) #找出所有整数['1', '-2', '60', '', '5', '-4', '3'] 9 10 #找到所有数字: 11 print(re.findall('D?(-?d+.?d*)',"1-2*(60+(-40.35/5)-(-4*3))")) # ['1','2','60','-40.35','5','-4','3'] 12 13 #计算器作业参考:http://www.cnblogs.com/wupeiqi/articles/4949995.html 14 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))' 15 16 content=re.search('(([-+*/]*d+.?d*)+)',expression).group() #(-3-40.0/5)
hsahlib模块
什么叫hash?
hash是一种算法,该算法接受传入的内容经过一定规则运算得到hash值
1只要传入的内容一样,得到的hash值必然一样
不能有hash值反解成内容
只要使用的hash算法不变,无论传入的内容多大,得到的hash值是固定的
1 1 import hashlib 2 2 3 3 m=hashlib.md5()# m=hashlib.sha256() 4 4 5 5 m.update('hello'.encode('utf8')) 6 6 print(m.hexdigest()) #5d41402abc4b2a76b9719d911017c592 7 7 8 8 m.update('alvin'.encode('utf8')) 9 9 10 10 print(m.hexdigest()) #92a7e713c30abbb0319fa07da2a5c4af 11 11 12 12 m2=hashlib.md5() 13 13 m2.update('helloalvin'.encode('utf8')) 14 14 print(m2.hexdigest()) #92a7e713c30abbb0319fa07da2a5c4af 15 15 16 16 ''' 17 17 注意:把一段很长的数据update多次,与一次update这段长数据,得到的结果一样 18 18 但是update多次为校验大文件提供了可能。