Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python re.search() Method

Python re.search() Method

Python re Module Python re Module


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

- Type: string (str) - Description: the regular expression pattern to match. - Type: string (str) - Description: the string to search, in which matches are looked for. - Type: integer (int, optional) - Description: regular expression flags that control the matching mode, such as `re.IGNORECASE`, `re.MULTILINE`, etc.

Function description


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:

   

print

(

"Match found:"

,

result.

group

(

)

)

 

Output: 3.9

   

print

(

"Match start position:"

,

result.

start

(

)

)

 

Output: 7

   

print

(

"Match end position:"

,

result.

end

(

)

)

 

Output: 10

Expected output:

找到匹配: 3.9
匹配起始位置: 7
匹配结束位置: 10

Code analysis:

  1. r'\d+\.\d+' Matches one or more digits, followed by a dot, followed by one or more digits (a version number format).
  2. re.search() Returns an object for the first match position.
  3. result.group() Returns the text content of the match.
  4. result.start() and result.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:

   

print

(

"First phone number:"

,

result.

group

(

)

)

Expected output:

第一个手机号: 138-1234-5678

Code analysis:

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:

   

print

(

"Found:"

,

result.

group

(

)

)

 

Output: Python

Expected output:

找到: Python

Code analysis:

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:

   

print

(

"Full match:"

,

result.

group

(

)

)

     

user@example.com

   

print

(

"Group 1:"

,

result.

group

(

1

)

)

   

user

   

print

(

"Group 2:"

,

result.

group

(

2

)

)

   

example.com

Expected output:

完整匹配: user@example.com
第1个分组: user
第2个分组: example.com

Code analysis:


Python re Module Python re Module