re模块

2019-01-23 07:41:42来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

import re
'''
\w : 匹配字母数字及下划线
\W :匹配非字母数字下划线
'''
print(re.findall('\w','ab 12\+- *&_'))   #['a', 'b', '1', '2', '_']
print(re.findall('\W','ab 12\+- *&_'))      #[' ', '\\', '+', '-', ' ', '*', '&']


'''
\s : 匹配任意空白字符,等价于[\t\n\r\f]
\S : 匹配任意非空字符
'''

print(re.findall('\s','ab \r1\n2\t\+- *&_'))  #[' ', '\r', '\t', ' ']
print(re.findall('\S','ab \r1\n2\t\+- *&_'))  #['a', 'b', '1', '2', '\\', '+', '-', '*', '&', '_']


'''
\d : 匹配任意数字,等价于[0-9]
\D : 匹配任意非数字
'''
print(re.findall('\d','ab \r1\n2\t\+-*&_'))  #['1', '2']
print(re.findall('\D','ab \r1\n2\t\+- *&_'))  #['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('sb\Z','alexsb is sbalexbsb'))    #['sb']
print(re.findall('sb$','alexsb is sbalexbsb'))     #['sb']

print(re.findall('^ebn$','ebn1'))


'''
\n : 匹配一个换行符
\t : 匹配一个制表符
'''
print(re.findall('a\nc','a\nc a\tc alc'))   #['a\nc']

'''重复匹配
.  ?  *  +  {m,n} .*(贪婪模式)  .*?(非贪婪模式)
'''
'''1, . 代表除了换行符外的任意一个字符'''
print(re.findall('a.c','abc alc aAc  aaaaaa\nc'))  #['abc', 'alc', 'aAc']
print(re.findall('a.c','abc alc aAc  aaaaaa\nc',re.DOTALL))   #['abc', 'alc', 'aAc', 'a\nc']

'''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('(alex)_sb','alex_sb sdfsdggfealex_sb'))  #['alex', 'alex']
print(re.findall(
    'href="(.*?)"',
    '<li><a id="blog_nav_sitehome" class="menu" href="http://www.cnblogs.com/">博客园</a></li>')  #['http://www.cnblogs.com/']
 )
# <li><a id="blog_nav_sitehome" class="menu" 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('([a-z]+)_sb','egon alex_sb1132wxxxxxxxxxxxx_sb,lxx_sb'))   #['alex', 'wxxxxxxxxxxxx', 'lxx']


'''|:a或者b'''
print(re.findall('compan(ies|y)','Too many companies have gone bankrupt, and the next one is my company'))   #['ies', 'y']


'''(?:):代表取匹配成功的所有内容,而不仅仅只是括号内的内容'''
print(re.findall('compan(?:ies|y)','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())   #sb
print(re.search('^alex','123123 alex sb asdfasdfegon alex sb egon'))        #None

print(re.search('^alex','alex sb sadfsadfasdfegon alex sb egon').group())   #alex
print(re.search('alex','alex sb sadfsadfasdfegon alex sb egon').group())     #alex
print(re.match('alex','alex sb sadfsadfasdfegon alex sb egon').group())     #alex
print(re.match('alex','123213 alex sb sadfsadfasdfegon alex sb egon'))      #None

info='a:b:c:d'
print(info.split(':'))     #['a', 'b', 'c', 'd']
print(re.split(':',info))  #['a', 'b', 'c', 'd']

info=r'get :a.txt\3333/rwx'
print(re.split('[:\\\/]',info))   #['get ', 'a.txt', '3333', 'rwx']

print('egon is beutifull egon'.replace('egon','EGON',1))    #EGON is beutifull egon
print(re.sub('(.*?)(egon)(.*?)(egon)(.*?)',r'\1\2\3EGON\5','123 egon is beutifull egon 123'))   #123 egon is beutifull EGON 123
#              (123 )(egon)( is beutifull )(egon)( 123)

print(re.sub('(lqz)(.*?)(SB)',r'\3\2\1',r'lqz is SB'))   #SB is lqz
print(re.sub('([a-zA-Z]+)([^a-zA-Z]+([a-zA-Z]+)(^a-zA-Z)+)([a-zA-Z]+)',r'\5\2\3\4\1',r'lqzzzz123 + is SB'))   #lqzzzz1 d 23 + sd32is SB
#(lqzzzz)(123+ )(is)( )(SB)

pattern=re.compile('alex')
print(pattern.findall('alex is alex alex'))   #['alex', 'alex', 'alex']
print(pattern.findall('alexsadfsasdfdsfdsfsd is alex alex'))  #['alex', 'alex', 'alex']
 

  


原文链接:https://www.cnblogs.com/HZLS/p/10302949.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:python数据类型分类以及运算类型

下一篇:用Python实现检测视频真伪?