正则匹配补充

match

从开头进行匹配,不匹配报错
def match(pattern, string, flags=0):
    """Try to apply the pattern at the start of the string, returning
    a match object, or None if no match was found."""
    return _compile(pattern, flags).match(string)

search

def search(pattern, string, flags=0):
    """Scan through string looking for a match to the pattern, returning
    a match object, or None if no match was found."""
    return _compile(pattern, flags).search(string)

注意点:
1、search是从整个字符串里查找

findall的注意点:

1、将字符串中所有符合要求的字符返回
2、返回的结果是一个列表类型
3、如果没有符合条件的,返回的是一个空列表
4、一般findall比search和match用的多

sub

ret = re.sub('\d{2}','19','age is 18,phone is 123',1)
print(ret)
#替换字符串'age is 18,phone is 123'中的俩位数字为19,替换次数为1

split

ret = re.split(' |,','age is 18,phone is 123')
print(ret)
#把字符串以|分割出来

贪婪匹配与非贪婪匹配

print(’######################################’)
re1 = re.match(’\d*?’,‘123456789’)
re1 = re.match(’\d+?’,‘123456789’)
print(re1.group())

注意点:
1、默认的状态是贪婪匹配,尽可能多的去匹配字符个数
2、在多个匹配符号后面,比如:* + {m,n}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值