首页 > 文章列表 > python re.match函数的使用

python re.match函数的使用

Python re.match
275 2022-08-07

1、从字符串的起始位置匹配正则表达式,re.match函数从string的起始位置开始匹配。

2、如果匹配失败则返回None,匹配成功则返回匹配到的字符串。

pattern是正则表达式,string是要匹配的字符串,flags是标志位。

re.match函数从string的起始位置开始匹配。

实例

import re
x=re.match("[1-9]\d*","123abd")
if x!=None:
    print(x.group())
else:
    print("none")
y=re.match("[1-9]\d*","c123ad")
if y!=None:
    print(y.group())
else:
    print("none")
#输出结果:
123
none

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。