import re
re.match()
re.match('www', 'www.runoob.com')
s = 'www.runoob.com'
s[0:3]
s = 'WWW.run123oo b\n.com'
s.replace('.','')
re.sub()
# 字母
re.sub('[a-zA-Z]','',s)
re.sub('[a-z]','',s,flags=re.I)
re.sub('(oob)','',s)
re.sub('^[wao]*','',s,flags=re.I) # 匹配开头
re.sub('[^o]','',s,flags=re.I) # 非o
re.sub('\s','',s) # 匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。
# 匹配结尾是$
# 数字
re.sub('[0-9]','a',s)
re.sub('\d','a',s) # \d 数字
re.sub('\D','a',s) # \D 非数字
# 中文
s = '推荐:59999人 不推荐:203人 一般推荐:10人'
re.findall('\d*',s)
re.findall('\d+',s)
re.findall('\d{2}',s)
re.findall('\d{2,4}',s)
# 去注释
s = 'select * from a -- zhushi \n where '
re.sub('(--).*','',s)
# *任意个数 [0,inf]
# +大于等于1个 [1,inf]
# ?0个或1个
# 找到字符串中的数字
str = " treeege-98711111 hhhh "
str = " +923husfda"
str = " hsfd783291fsdjao"
re.findall('[+-]?\d+',str)
# 去空格
str.lstrip()
str.rstrip()
str.strip()
# [] 任意字符
# () 整个字符串
lzy = "我的邮箱是: 123439167@qq.com,我的手机是: 18814107346 你的呢"
mxq = "我的手机是: 15820230994,我还有一个假的手机号8783921,我的邮箱是abc@126.com"
email_lzy = re.findall('[a-z0-9]+@[a-z0-9]+\.[a-z0-9]+', lzy, flags=re.I)
mobile_phone_lzy = re.findall('[0-9]{11}', lzy, flags=re.I)
email_mxq = re.findall('[a-z0-9]+@[a-z0-9]+\.[a-z0-9]+', mxq, flags=re.I)
mobile_phone_mxq = re.findall('[0-9]{11}', mxq, flags=re.I)
gz = '广州固话:020-8122888,广州邮箱:aa1135-66@qq.com'
sz = '深圳:0200-88887777,深圳邮箱:bb22-11@qq.com'
gz_phone = re.findall('[0-9]{3,4}\-[0-9]{6,8}',gz)
sz_phone = re.findall('[0-9]{3,4}\-[0-9]{6,8}',sz)
s = "'select * \nfrom a \n where /*这是第一行注释\n第二行注释\n第三行注释\n */ \n dep_date ='2019-12-01' /*补充注释*/"
s_new = s.replace('\n','')
re.sub('(/\*).*?(\*/)', '', s.replace('\n', ''))
re.sub('(/\*).+?(\*/)', '', s.replace('\n', ''))
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 将匹配的数字乘以 2
def double(matched):
value = int(matched.group('value'))
return str(value * 2)
s = 'A23G4HFD567'
print(re.sub('(?P<value>\d+)', double, s))
s.group()
re.findall('(?P<value>\d+)', s)