Python re.search() Method
re.search() is a function in the Python re module used toSearch for a patternfunction that searches within the stringScan and searchFind the first position that matches the specified regular expression.
The difference between re.match() in Vue 2,re.search() It does not require a match at the beginning of the string; it looks for a match anywhere in the string.
Word meaning: search means "to search".
Basic syntax and parameters
re.search() is a module-level function, called directly through the re module.
Syntax format
re.search(pattern, string, flags=0)
Parameter Description
- pattern:
- Type: string (str)
- Description: the regular expression pattern to match.
- string:
- Type: string (str)
- Description: the string to search, in which matches are looked for.
- flags:
- Type: integer (int, optional)
- Description: regular expression flags that control the matching mode, such as
re.IGNORECASE、re.MULTILINE, etc.
Function description
- Return Value: If a match is found, return a
matchobject; if no match is found, returnNone。 - effect: Returns the position information of the first match, including the matched content, start position and end position.
Example
Let us thoroughly master re.search() through a series of examples from simple to complex.
Example 1: basic usage - find the first match
Example
import
re
Search the string for the first matching number
text
=
"Python 3.9 is the most commonly used version"
result
=
re
.
search
(
r
'\d+.\d+'
,
text
)
if
result:
(
"Match found:"
,
result.
group
(
)
)
Output: 3.9
(
"Match start position:"
,
result.
start
(
)
)
Output: 7
(
"Match end position:"
,
result.
end
(
)
)
Output: 10
Expected output:
找到匹配: 3.9
匹配起始位置: 7
匹配结束位置: 10
Code analysis:
r'\d+\.\d+'Matches one or more digits, followed by a dot, followed by one or more digits (a version number format).re.search()Returns an object for the first match position.result.group()Returns the text content of the match.result.start()andresult.end()Returns the position indices of the match.
Example 2: find a match at any position
re.search() Matches can be found anywhere in the string; this is the main difference from re.match() .
Example
import
re
text
=
"My phone number is 138-1234-5678, his phone number is 139-9876-5432"
Find the first phone number
result
=
re
.
search
(
r
'\d{3}-\d{4}-\d{4}'
,
text
)
if
result:
(
"First phone number:"
,
result.
group
(
)
)
Expected output:
第一个手机号: 138-1234-5678
Code analysis:
\d{3}-\d{4}-\d{4}Matches the Chinese mobile phone number format (3 digits-4 digits-4 digits).re.search()It returns as soon as the first match is found and does not keep searching.
Example 3: use flags to ignore case
Pass flags The parameter can change matching behavior, such as ignoring case.
Example
import
re
text
=
"Python is a Powerful Programming Language"
Use re.IGNORECASE to ignore case
result
=
re
.
search
(
r
'python'
,
text
,
re
.
IGNORECASE
)
if
result:
(
"Found:"
,
result.
group
(
)
)
Output: Python
Expected output:
找到: Python
Code analysis:
re.IGNORECASEMakes the match case-insensitive, so "Python" can be matched.
Example 4: extract group contents
Using parentheses () can create groups to extract the specific parts you are interested in.
Example
import
re
text
=
"User email: user@example.com"
Use groups to extract the username and the domain
result
=
re
.
search
(
r
'(\w+)@(\w+.\w+)'
,
text
)
if
result:
(
"Full match:"
,
result.
group
(
)
)
user@example.com
(
"Group 1:"
,
result.
group
(
1
)
)
user
(
"Group 2:"
,
result.
group
(
2
)
)
example.com
Expected output:
完整匹配: user@example.com
第1个分组: user
第2个分组: example.com
Code analysis:
- Parentheses
()Two groups are created. result.group(1)andresult.group(2)Extract the contents of the two groups respectively.
Python re Module