python3 re模块_Python3基础笔记---re模块

importre## . 通配符,一个 . 模糊匹配一个除换行符之外的任意字符#ret = re.findall('a..in', 'helloalvin')#print(ret) # ['alvin']#

## ^ 必须以某个字符开始#ret = re.findall('^a...n', 'alvinhelloawwwn')#print(ret) # ['alvin']#

## $ 必须以某个字符结尾#ret = re.findall('a...n$', 'alvinhelloawwwn')#print(ret) # ['awwwn']#

## * 贪婪匹配 [0, +oo]#ret = re.findall('abc*', 'abcccc') # 贪婪匹配[0,+oo]#print(ret) # ['abcccc']#

#ret = re.findall('a.*n', 'alvinhelloawwwn')#print(ret) # ['alvinhelloawwwn'] 贪婪匹配#

## + 贪婪匹配 [1, +oo]#ret = re.findall('abc+', 'abccc') # [1,+oo]#print(ret) # ['abccc']#

#ret = re.findall('a.+n', 'alvinhelloawwwn')#print(ret) # ['alvinhelloawwwn'] 贪婪匹配#

## ? 匹配[0,1]#ret = re.findall('abc?', 'abcccffab') # [0,1]#print(ret) # ['abc', 'ab']#

#{} 自定义重复次数 {1,} 表示一到正无穷

ret = re.findall('abc{1,4}', 'abcccccsccccc')print(ret) #['abcccc'] 贪婪匹配

#--------------------------------------------字符集[]

#ret = re.findall('a[bc]d', 'acd')#print(ret) # ['acd'] 匹配 b 或 c#

#ret = re.findall('[a-z]', 'acd')#print(ret) # ['a', 'c', 'd']#

#ret = re.findall('[.*+]', 'a.cd+')#print(ret) # ['.', '+'] # 在[]中,* + 失去原有的作用#

#在字符集里有功能的符号: - ^ \#

#ret = re.findall('[1-9]', '45dha3')#print(ret) # ['4', '5', '3']#

#^ 放在[]表示取反,不取 a 或 b 或 ,#ret = re.findall('[^ab,]', '45bdha3,')#print(ret) # ['4', '5', 'd', 'h', '3']#

#ret = re.findall('[\d]', '45bdha3')#print(ret) # ['4', '5', '3']

#\ 的功能#1、反斜杠后面跟元字符去除其特殊功能#2、反斜杠后面跟普通字符实现其特殊功能

'''\d 相当于 [0-9]

\D 相当于 [^0-9]

\s 匹配任何空白字符

\S 匹配任何非空字符

\w 匹配任何字母数字字符 [0-9a-zA-Z]

\W 匹配任何非字母数字字符 [^0-9a-zA-Z]

\b 匹配一个特殊字符边界,也就是指单词和空格间的位置'''

#ret=re.findall('I\b','I ')#print(ret)#[]

ret= re.findall('\dert','13ert')print(ret) #['3ert']

ret= re.findall('\Dert','13^ert')print(ret) #['^ert']

ret= re.findall('\s123', '123')print(ret)print(re.findall(r'I\b', 'hello,I am a hhI$hh'))print(re.findall(r'\bI', 'hello, I am a hhI$hh'))print(re.findall(r'\\', r'abf\vaf'))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值