Re.searchScans the entire string and returns the first successful match, returns the Match object if the string contains the pattern substring, otherwise returns none, and note that if there are multiple pattern substrings in a string, only the first one is returned.
Re.search () methodUsed to precisely match and extract the first conforming object, while extracting the object content uses
Search MethodProperty Group () to implement the.
function Syntax:
Re.search (Pattern, string, flags=0)
Function parameter Description:
| Parameters |
Describe |
| Pattern |
Matched regular Expressions |
| String |
The string to match |
| Flags |
A flag bit that controls how regular expressions are matched, such as case sensitivity, multiline matching, and so on. |
The match succeeds Re.search method returns a matching object, otherwise none is returned.
We can use the group (NUM) or groups () matching object function to get a matching expression.
| Match object method |
description |
| group (num=0) |
groups () |
Returns a tuple containing all the group strings, from 1 to the included group number. |
Instance
#!/usr/bin/python#-*-coding:utf-8-*-Import reprint (re.search (' www ', ' www.runoob.com '). span ()) # matches print at the starting position ( Re.search (' com ', ' www.runoob.com '). span ()) # does not match at the start position
The above example runs the output as:
(0, 3) (11, 14)
In-Class extension:
What is the Python re.match function and read through the use of the Python match function
A Python regular expression that tells you what a python regular expression is?