Home >Backend Development >Python Tutorial >What is the python re.match function? Learn how to use the python match function.

What is the python re.match function? Learn how to use the python match function.

Tomorin
TomorinOriginal
2018-08-15 13:53:046647browse


#With the previous article "Detailed explanation of Python regular expressions, tell you what are Python regular expressions?" 》Linkage, this article describes the method of using the python re.match function, and comes with tables and examples to analyze the use of the Python match function.

What is the python re.match function? Learn how to use the python match function.

re.match Try to match a pattern from the starting position of the string. If the matching is not successful at the starting position, match() will return none.

Related recommendations: "

Python Tutorial"

Function syntax:

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

Function parameter description:

ParameterDescriptionpatternMatching regular expressionstringThe string to match. #flags Flag bit is used to control the matching method of regular expressions, such as: whether to be case-sensitive, multi-line matching, etc.


The re.match method returns a matching object if the match is successful, otherwise it returns None.

We can use group(num) or groups() matching object function to get the matching expression.

Match object methodDescriptiongroup(num=0) Matches the string of the entire expression. group() can enter multiple group numbers at once, in which case it will return a tuple containing the values ​​corresponding to those groups. groups()Returns a tuple containing all group strings, from 1 to the contained group number.


Example:


#!/usr/bin/python
# -*- coding: UTF-8 -*- 
import re
print(re.match('www', 'www.runoob.com').span())  # 在起始位置匹配
print(re.match('com', 'www.runoob.com'))       # 不在起始位置匹配

The output result of the above example is:

(0, 3)
None


The above is the detailed content of What is the python re.match function? Learn how to use the python match function.. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn