python re模块学习(1)

来源:互联网 发布:误删除 mysql数据恢复 编辑:程序博客网 时间:2024/05/21 20:24

1:* 表示匹配最后一个字符,有多少就返回多少
例:

import reresult = re.match(r'abc*','abcccc')>>> result.group()='abcccc'

如例:re里最后的字符c有多少个,就匹配多少个,如果一个都没有,就只返回之前的字符
2:+ 表示匹配最后一个字符,至少要有1个,有多少返回多少

import reresult = re.match(r'abc+','abcccc')>>> result.group()='abcccc'
import reresult = re.match(r'abc+','abdfg')>>> result.group()>AttributeError: 'NoneType' object has no attribute 'group'

如例,一个c都找不到的话,就返回Nonetype

如果是非贪婪模式的话,就可以在上2个例子直接加?
例:

import reresult = re.match(r'abc+?','abccc')>>> result.group()>'abc'

返回的字符串只匹配一次最后的一个字符‘c’

import reresult = re.match(r'abc*?','abccc')>>> result.group()>'abc'

返回的字符串只匹配0次最后的一个字符‘c’


原创粉丝点击