二. re模块函数详解

1. compile(pattern, flag)

import re
c = re.compile('abc')
type(c) # -> <class '_sre.SRE_Pattern'>
c       # -> re.compile('abc') 

备注:
1.1 re.compile() 返回一个 预编译过的正则表达式对象。
1.2 方法文档

    compile(pattern, flags=0)

    Compile a regular expression pattern, returning a pattern object.

2. match(pattern, string, flags=0)

import re
m = re.match('foo', 'foo')
type(m) # -> <class '_sre.SRE_Match'>
m       # -> <_sre.SRE_Match object; span=(0, 3), match='foo'>
m is None # -> False    

备注:
2.1 match() 试图从字符串起始部分对模式进行匹配,如果匹配成功,则返会匹配对象,如果匹配失败,则返回None.
2.2 在实际操作中,最好不要省略if判断,都在可能包AttributeError异常
2.2 官方文档

    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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值