在这篇文章之中我们来了解一下关于python之中的正则表达式,有些朋友可能是刚刚接触到python这一编程语言,对于这一方面不是特别的了解,在接下来的文章之中我们来了解一下python中re.match函数,python re.match函数是python中常用的正则表达式处理函数。废话不多说,我们开始进入文章吧。
re.match函数:
re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none。
函数的语法
re.match(pattern, string, flags=0)
函数参数说明:
匹配成功re.match方法返回一个匹配的对象,否则返回none。
我们可以使用group(num) 或 groups() 匹配对象函数来获取匹配表达式。
实例如下:
#!/usr/bin/python# -*- coding: utf-8 -*- import reprint(re.match('www', 'www.runoob.com').span()) # 在起始位置匹配print(re.match('com', 'www.runoob.com')) # 不在起始位置匹配
输出如下:
(0, 3)none
# !/usr/bin/pythonimport reline = "cats are smarter than dogs"matchobj = re.match(r'(.*) are (.*?) .*', line, re.m | re.i)if matchobj: print "matchobj.group() : ", matchobj.group() print "matchobj.group(1) : ", matchobj.group(1) print "matchobj.group(2) : ", matchobj.group(2)else: print "no match!!"
以上实例输出如下:
matchobj.group() : cats are smarter than dogsmatchobj.group(1) : catsmatchobj.group(2) : smarter
以上就是本篇文章所讲述的所有内容,这篇文章主要介绍了python中re.match函数的相关知识,希望你能借助资料从而理解上述所说的内容。希望我在这片文章所讲述的内容能够对你有所帮助,让你学习python更加轻松。
更多相关知识,请访问python教程栏目。
以上就是什么是python re.match函数?(实例解析)的详细内容。