Python正则表达式学习例子

(1)匹配1-100之间的数

import re 

s = '100'   # 1-100内的任意数字
ret = re.match(r'(100|[1-9]\d{0,1})$',s)
print(ret.group())

 

(2)匹配座机号码

010-67132692,其构造规则为[3位数字][-][8位数字]

或者

0431-4978981,其构造规则为[4位数字][-][7位数字]

import re 

s = "010-67132692"
ret = re.search(r'^\d{3,4}-\d{7,8}$' , s)
print(ret.group())

 

(3)对输入的qq号进行匹配(qq匹配规则:长度为5-10位,纯数字组成,且不能以0开头。) 

import re 

s = "1101111123"
ret = re.match(r'[1-9]\d{4,9}$' , s)
if ret != None:
    print(ret.group())
else :
    print('匹配失败!')

 

(4)查找字符串中有多少个af

import re 
s = "asdfjvjadsffvaadfkfasaffdsasdffadsafafsafdadsfaafd"

ret = re.findall(r'(af)' , s)
print(len(ret))

 

(5)规则是按照空格出现一次或者多次切割

import re 

s = "zhangsan   lisi  wangwu"
res = re.compile(r'\s+')
ret = res.split(s)
print(ret)

效果图:

 

(6)用正则\\切割

import re 

s = "c:\\abc\\a.txt"
res = re.compile(r'\\')
ret = res.split(s)
print(ret)

效果图:

 

(7)将连续5个以上数字替换成#

import re 

s = "wer8934605juo123wa89320571f"
res = re.compile(r'\d{5,}')
ret = res.sub('#' , s)
print(ret)

效果图:

 

(8)取出字符串中的所有字母 

import re 

s = "abDEe23dJfd343dPOddfe4CdD5ccv!23rr"
res = re.compile(r'[a-zA-Z]+')
ret = res.findall(s)
print(ret)

效果图:

 

(9)找出以字母e结尾的单词,忽略大小写 

import re 

s = 'THREE people at HERE do some THING'
res = re.compile(r'\w+e\b' , re.I)   #\b为边界
ret = res.findall(s)
print(ret)

效果图:

 

 (10)将多个重复字母替换成&

import re 

s = "cudddbhuuujdddcaa"
res = re.compile(r'([a-zA-Z])\1+')
ret = res.sub('&' , s)
print(ret)

效果图:

 

(11)将多个重复字母替换成一个字母(比如ddd替换成d)

import re 

s = "cudddbhuuujddd"
res = re.compile(r'([a-zA-Z])\1+')
ret = res.sub(r'\1',s)
print(ret)

效果图:

 

(11)获取长度为3个字母的单词

import re 

s = "min tian jiu yao fang jia le ,da jia"
ret = re.findall(r'\b\w{3}\b' , s)
print(ret)

效果图:

 

(12)将字符串变成 '我要学编程'

s = "我我...我我...我要..要要...要要...学学学...学学...编编编..编程..程.程...程...程"
res = re.sub(r'\W+','', s)
ret = re.sub(r'(.)\1+',r'\1',res)     
print(ret)

效果图:

 

(13)去掉div和b标签

结果:正则表达式练习

import re 

s = "
正则表达式练习
" ret = re.sub(r'(|)','',s) print(ret)

效果图:

 

(14)找出每行中只有3个数字的字符串 

s = '''121fefe
3qsqse2
ded6d32
aaaaa1a
1234adc
'''
ret = re.findall(r'^\D*\d\D*\d\D*\d\D*$' , s ,re.M)
print(ret)

效果图:

 

 

 

你可能感兴趣的:(Python基础)