re模块

2019-09-23 03:02 来源:未知
import re'''w : 匹配字母数字及下划线W :匹配非字母数字下划线'''print(re.findall('w','ab 12 - *&_'))   #['a', 'b', '1', '2', '_']print(re.findall('W','ab 12 - *&_'))      #[' ', '\', ' ', '-', ' ', '*', '&']'''s : 匹配任意空白字符,等价于[tnrf]S : 匹配任意非空字符'''print(re.findall('s','ab r1n2t - *&_'))  #[' ', 'r', 't', ' ']print(re.findall('S','ab r1n2t - *&_'))  #['a', 'b', '1', '2', '\', ' ', '-', '*', '&', '_']'''d : 匹配任意数字,等价于[0-9]D : 匹配任意非数字'''print(re.findall('d','ab r1n2t -*&_'))  #['1', '2']print(re.findall('D','ab r1n2t - *&_'))  #['a', 'b', ' ', 'r', 'n', 't', '\', ' ', '-', ' ', '*', '&', '_']print(re.findall('w_sb','exxx absx_sb123123wxx_sb,lxx_sb'))  #['x_sb', 'x_sb', 'x_sb']'''A :匹配字符串开始'''print(re.findall('Aalex','asdfalex is salexsb'))      #[]print(re.findall('Aalex','alex is salexsb'))       #['alex']'''Z : 匹配字符串结束,如果是存在换行,只匹配到换行前的结束字符串z : 匹配字符串结束G :匹配最好匹配完成的位置^ :  匹配字符串的开头$ :  匹配字符串的末尾'''print(re.findall('^alex','alex is salexb'))        #['alex']print(re.findall('sbZ','alexsb is sbalexbsb'))    #['sb']print(re.findall('sb$','alexsb is sbalexbsb'))     #['sb']print(re.findall('^ebn$','ebn1'))'''n : 匹配一个换行符t : 匹配一个制表符'''print(re.findall('anc','anc atc alc'))   #['anc']'''重复匹配.  ?  *     {m,n} .*  .*?''''''1, . 代表除了换行符外的任意一个字符'''print(re.findall('a.c','abc alc aAc  aaaaaanc'))  #['abc', 'alc', 'aAc']print(re.findall('a.c','abc alc aAc  aaaaaanc',re.DOTALL))   #['abc', 'alc', 'aAc', 'anc']'''2,? : 代表左边那一个字符重复0次或1次,非贪婪模式'''print(re.findall('ab?','a ab abb abbb abbbb abbbbbb'))  #['a', 'ab', 'ab', 'ab', 'ab', 'ab']'''3, *:代表左边那一个字符出现0次或无穷次'''print(re.findall('ab*','a ab abb abbb abbbb abbbbbb albbbbbbbb'))  #['a', 'ab', 'abb', 'abbb', 'abbbb', 'abbbbbb', 'a']'''4, :代表左边那一个字符出现1次或无穷次'''print(re.findall('ab ','a ab abb abbb abbbb abbbbbb albbbbbbbb'))  #['ab', 'abb', 'abbb', 'abbbb', 'abbbbbb']'''5,{m,n}:代表左边那一个字符出现m次到n次'''print(re.findall('ab?','a ab abb abbb abbbb abbbbbb albbbbbbbb'))print(re.findall('ab{0,1}','a ab abb abbb abbbb abbbbbb albbbbbbbb'))  #['a', 'ab', 'ab', 'ab', 'ab', 'ab', 'a']print(re.findall('ab*','a ab abb abbb abbbb abbbbbb albbbbbbbb'))print(re.findall('ab{0,}','a ab abb abbb abbbb abbbbbb albbbbbbbb'))  #['a', 'ab', 'abb', 'abbb', 'abbbb', 'abbbbbb', 'a']print(re.findall('ab ','a ab abb abbb abbbb abbbbbb albbbbbbbb'))print(re.findall('ab{1,}','a ab abb abbb abbbb abbbbbb albbbbbbbb'))  #['ab', 'abb', 'abbb', 'abbbb', 'abbbbbb']print(re.findall('ab{1,3}','a ab abb abbb abbbb abbbbbb albbbbbbbb'))  #['ab', 'abb', 'abbb', 'abbb', 'abbb']'''6,.*:匹配任意长度,任意的字符======》贪婪匹配'''print(re.findall('a.*b','ac a123c aaaaaacbb a *123) ()c asb dfsdjfdsf'))  #['ac a123c aaaaaacbb a *123) ()c asb']'''7,.*?: 非贪婪模式'''print(re.findall('a.*?c','a123c454c'))  #['a123c']'''8,():分组'''print(re.findall_sb','alex_sb sdfsdggfealex_sb'))  #['alex', 'alex']print(re.findall(    'href=""',    '<li><a   href="http://www.cnblogs.com/">博客园</a></li>')  #['http://www.cnblogs.com/'] )# <li><a   href="http://www.cnblogs.com/">博客园</a></li>#                                           href=".*?"'''[]:匹配一个指定范围内的字符(这一个字符来自括号内定义的)'''print(re.findall('a[0-9]c','alc a c a2c a9c a11c a-c acc aAc'))         #['a2c', 'a9c']print(re.findall('a[0-9][0-9]c','alc a c a2c a9c a11c a-c acc aAc'))     #['a11c']print(re.findall('a[0-9] c','alc a c a2c a9c a11c a-c acc aAc'))         #['a2c', 'a9c', 'a11c']'''当-需要被当中普通符号匹配时,只能放到[]的最左边或最右边'''print(re.findall('a[- *]c','alc a c a2c a9c a11c a-c a*cc aAc'))     #['a c', 'a-c', 'a*c']print(re.findall('a[a-zA-Z]c','alc a c a2c a9c a11c a-c a*cc aAc'))  #['alc', 'aAc']'''[]内的^代表取反的意思'''print(re.findall('a[^a-zA-Z]c','a c alc a c allc a-c acc aAc'))             #['alc', 'aAc']print(re.findall('a[^0-9] c','a c alc a8c a899c a c allc a-c acc aAc'))      #['a c alc', 'a c allc a-c acc aAc']print(re.findall_sb','egon alex_sb1132wxxxxxxxxxxxx_sb,lxx_sb'))   #['alex', 'wxxxxxxxxxxxx', 'lxx']'''|:a或者b'''print(re.findall('compan','Too many companies have gone bankrupt, and the next one is my company'))   #['ies', 'y']''':代表取匹配成功的所有内容,而不仅仅只是括号内的内容'''print(re.findall('compan','Too many companies have gone bankrupt, and the next one is my company'))print(re.findall('alex|sb','alex sb sadfalexsbdfasdfegon alex sb egon'))    #['alex', 'sb', 'alex', 'sb', 'alex', 'sb']'''re模块的其他方法:'''print(re.findall('alex|sb','123123 alex sb asdfasdfegon alex sb egon'))     #['alex', 'sb', 'alex', 'sb']print(re.search('alex|sb','123123sb alex sb asdfasdfegon alex sb egon').group   #sbprint(re.search('^alex','123123 alex sb asdfasdfegon alex sb egon'))        #Noneprint(re.search('^alex','alex sb sadfsadfasdfegon alex sb egon').group   #alexprint(re.search('alex','alex sb sadfsadfasdfegon alex sb egon').group     #alexprint(re.match('alex','alex sb sadfsadfasdfegon alex sb egon').group     #alexprint(re.match('alex','123213 alex sb sadfsadfasdfegon alex sb egon'))      #Noneinfo='a:b:c:d'print(info.split     #['a', 'b', 'c', 'd']print(re.split)  #['a', 'b', 'c', 'd']info=r'get :a.txt3333/rwx'print(re.split('[:\/]',info))   #['get ', 'a.txt', '3333', 'rwx']print('egon is beutifull egon'.replace('egon','EGON',1))    #EGON is beutifull egonprint(re.sub',r'123EGON5','123 egon is beutifull egon 123'))   #123 egon is beutifull EGON 123#              ( is beutifull )print(re.sub',r'321',r'lqz is SB'))   #SB is lqzprint(re.sub('([a-zA-Z] )([^a-zA-Z] ([a-zA-Z] ) )([a-zA-Z] )',r'52341',r'lqzzzz123   is SB'))   #lqzzzz1 d 23   sd32is SB#pattern=re.compileprint(pattern.findall('alex is alex alex'))   #['alex', 'alex', 'alex']print(pattern.findall('alexsadfsasdfdsfdsfsd is alex alex'))  #['alex', 'alex', 'alex'] 

  

版权声明:本文由韦德娱乐1946_韦德娱乐1946网页版|韦德国际1946官网发布于网络编程,转载请注明出处:re模块