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
Release: 2019-04-01 15:20:38
Original
6648 people have browsed it


#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)
Copy after login

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'))       # 不在起始位置匹配
Copy after login

The output result of the above example is:

(0, 3)
None
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template