Home  >  Article  >  Backend Development  >  Detailed explanation of python re.search method in Python

Detailed explanation of python re.search method in Python

Tomorin
TomorinOriginal
2018-08-15 13:54:0920432browse

re.search Scan the entire string and return the first successful match. If the string contains the pattern substring, a Match object is returned, otherwise None is returned. Note that if there are multiple strings, pattern substring, only the first one is returned. re.search() method is used to accurately match and extract the first object that conforms to the rules, and the extraction of object content is implemented using the attribute group() of the search method.

Function syntax:

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

Function parameter description:

##ParameterDescriptionpattern Matching regular expressionstringString to matchflags Flag bit, used to control the matching method of regular expressions, such as: whether to be case-sensitive, multi-line matching, etc.
#The re.search 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 methodDescription##group(num=0)groups()Example
Match 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.
Returns a tuple containing all group strings, from 1 to the contained group number.
#!/usr/bin/python
# -*- coding: UTF-8 -*- 
import re
print(re.search('www', 'www.runoob.com').span())  # 在起始位置匹配
print(re.search('com', 'www.runoob.com').span())  # 不在起始位置匹配

The output result of the above example is:

(0, 3)
(11, 14)

In-class extension:


What is the python re.match function, read through to understand the use of the python match function

Detailed explanation of Python regular expressions, tell you what Python regular expressions are?

The above is the detailed content of Detailed explanation of python re.search method in Python. 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