re模块


print(re.findall('\w','hello 123_ */-=')) #匹配所有字母,数字,下划线
print(re.findall('\W','hello 123_ */-=')) # 匹配所有非字母,数字,下划线

print(re.findall('\s','hell\no 12\t3_ */-=')) # 匹配任意空白字符
print(re.findall('\S','hell\no 12\t3_ */-=')) # 匹配任意非空字符

print(re.findall('\d','hell\no 12\t3_ */-=')) # 匹配任意数字
print(re.findall('\D','hell\no 12\t3_ */-=')) # 匹配任意非数字

print(re.findall('\n','hell\no \n12\t3_ */-=')) # 只匹配\n
print(re.findall('\t','hell\no 12\t3_ */-='))
print(re.findall('l','hell\no 12\t3_ */-='))


print(re.findall('egon','my name is egon,egon is beautiful')) # 匹配不到就返回空列表

print(re.findall('^legon my','legon my name is egon,egon is beautiful')) # ^匹配字符串的开头[lengon my]
print(re.findall('egon$','egon my name is egon,egon is beautifulegon')) # 匹配字符串的末尾
print(re.findall('n$','egon my name is egon,egon is beautifulegon')) # 匹配字符或字符串的末尾
#


重复匹配
.:匹配换行符以外的任意一个字符
print(re.findall('a.c','abc a1c aac asd aaaaac a*c a+c abasd')) #['abc','a1c','aac','aac','a*c','a+c'] 换行符占一个字符
a.c
print(re.findall('a.c','abc a1c aac a\nc asd aaaaac a*c a+c abasd',re.DOTALL)) # 加上re.DOTALL也可以匹配换行符

[]:匹配一个字符,该字符属于中括号内指定的字符
print(re.findall('a..c','abc a1 c aac asd aaaaac a *c a+c abasd =')) # ['a1 c', 'aaac', 'a *c']
print(re.findall('a.c','abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c'))
print(re.findall('a[a-z]c','abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c'))
print(re.findall('a[A-Z]c','abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c'))
print(re.findall('a[-+*/]c','abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c')) # 这里注意-的位置,放在前面或后面,放在中间被当成到的意思
print(re.findall('a[a-z][a-z]c','abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c'))
print(re.findall('a[^a-z]c','abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c')) # 注意,这里是不在[]中的字符


*: 必须与其他字符连用,代表左侧的字符出现0次或者无穷次
print(re.findall('ab*','a ab abbb abbbb a1bbbb a-123'))

['a','ab','abbb','abbbb','a','a']
print(re.findall('ab{0,}','a ab abbb abbbb a1bbbb a-123'))



?: 必须与其他字符连用,代表左侧的字符出现0次或者1次
print(re.findall('ab?','a ab abbb abbbb a1bbbb a-123'))
ab?
['a','ab','ab','ab','a','a']
print(re.findall('ab{0,1}','a ab abbb abbbb a1bbbb a-123'))



+: 必须与其他字符连用,代表左侧的字符出现1次或者无穷次
print(re.findall('ab+','a ab abbb abbbb a1bbbb a-123'))
ab+
['ab','abbb','abbbb']
print(re.findall('ab{1,}','a ab abbb abbbb a1bbbb a-123'))


{n,m}: 必须与其他字符连用
print(re.findall('ab{1,3}','a ab abbb abbbb a1bbbb a-123'))
ab{1,3}
['ab','abbb','abbb']


.*:贪婪匹配
print(re.findall('a.*c','ab123adfc1134124123adasfc123123'))

.*?:非贪婪匹配 匹配到最近的c就停止,再进行下次匹配
print(re.findall('a.*?c','ab123adfc1134124123adasfc123123'))
a.*?c


():分组 匹配到结果返回的字符是括号里的
print(re.findall('expression="(.*?)"','expression="1+2+3/4*5" egon="beautiful"'))
expression=".*?"


print(re.findall('href="(.*?)"','<p>段落</p><a href="https://www.sb.com">点我啊</a><h1>标题</h1><a href="https://www.sb.com">点我啊</a>'))



|: 匹配到a或b都会放到列表里
print(re.findall('a|b','ab123abasdfaf'))
a|b

print(re.findall('compan(?:ies|y)','Too many companies have gone bankrupt, and the next one is my company'))
匹配到括号里的字符串会和外面的连起来,结果如下
companies company



print(re.findall(r'a\\c','a\c a1c aAc aac'))
print(re.findall('a\\\\c','a\c a1c aAc aac'))

print(re.findall('ale(x)','alex is SB,alex is bigSB'))
print(re.search('alex','alex is SB,alex is bigSB')) # search 和 match都是从头开始匹配
print(re.search('ale(x)','alex is SB,alex is bigSB').group())
print(re.search('abcdefg','alex is SB,alex is bigSB'))

print(re.search('alex','123alex is SB,alex is bigSB')) # 找到匹配字符串的位置,group()可以输出
print(re.match('alex','alex is SB,alex is bigSB')) # search是匹配到第一个就停下来,match是开头匹配不到就返回None

l='egon:18:male'.split(':') # split不可以用下面的方法
print(l)
l1=re.split('[ :/-]','a-b/c egon:18:male xxx')
print(l1)

print(re.sub('[a-z]+xx','yxp','lxx is good,sb is lllxx wxx is good cxx is good'))
[a-z]+xx

pattern=re.compile('alex') 存成一个对象 可以无限次调用
print(pattern.findall('alex is SB,alex is bigSB'))
print(pattern.search('alex is SB,alex is bigSB'))

print('===>',re.sub('^(\w+)(.*?\s)(\w+)(.*?\s)(\w+)(.*?)$',r'\5\2\3\4\1','alex make bed'))
print('===>',re.subn('a','A','alex make love')) # 显示替换的字符个数


posted @ 2018-06-21 19:15  Roc_Atlantis  阅读(127)  评论(0编辑  收藏  举报