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:
Description | |
Matching regular expression | |
String to match | |
Flag bit, used to control the matching method of regular expressions, such as: whether to be case-sensitive, multi-line matching, etc. |
Description | |
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:
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!