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

Python re.fullmatch() Method

Python re.fullmatch() Method

Python re Module Python re Module


re.fullmatch() is a function in the Python re module used toFull matcha function for the entire string.

The difference between re.match() in Vue 2,re.fullmatch() Requires the entire string to match the regular expression, rather than matching only from the beginning.

Word meaning: full match means "full match".


Basic syntax and parameters

re.fullmatch() is a module-level function, called directly through the re module.

Syntax format


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

Parameter Description

- Type: string (str) - Description: the regular expression pattern to match. - Type: string (str) - Description: the string to match. - Type: integer (int, optional) - Description: regular expression flags.

Function description


Example

Let's master it through a series of examples re.fullmatch() through a series of examples from simple to complex.

Example 1: basic usage - full match

Example

import

re

Match the entire string completely

text

=

"hello"

result

=

re

.

fullmatch

(

r

'hello'

,

text

)

if

result:

   

print

(

"Full match succeeded:"

,

result.

group

(

)

)

Expected output:

完整匹配成功: hello

Code analysis:

Example 2: when full matching fails

If the string has extra characters, the match fails.

Example

import

re

text

=

"hello world"

Try to fully match "hello"

result

=

re

.

fullmatch

(

r

'hello'

,

text

)

if

result:

   

print

(

"Match succeeded:"

,

result.

group

(

)

)

else

:

   

print

(

"Match failed: the string is not exactly 'hello'"

)

Expected output:

匹配失败: 字符串不是完全等于 'hello'

Code analysis:

Example 3: using regular expression patterns

fullmatch The full regular expression syntax is supported.

Example

import

re

Fully match digits

text

=

"12345"

result

=

re

.

fullmatch

(

r

'\d+'

,

text

)

if

result:

   

print

(

"Digits matched:"

,

result.

group

(

)

)

Fully match letters

text2

=

"abc"

result2

=

re

.

fullmatch

(

r

'[a-z]+'

,

text2

)

if

result2:

   

print

(

"Letters matched:"

,

result2.

group

(

)

)

Expected output:

数字匹配: 12345
字母匹配: abc

Example 4: validating fixed formats

fullmatch Often used to check whether an input conforms to a specific format.

Example

import

re

Validate a phone number format

phone

=

"138-1234-5678"

result

=

re

.

fullmatch

(

r

'\d{3}-\d{4}-\d{4}'

,

phone

)

if

result:

   

print

(

f

"Phone number {phone} has a valid format"

)

else

:

   

print

(

f

"Phone number {phone} has an invalid format"

)

Validate email format

email

=

"test@example.com"

result2

=

re

.

fullmatch

(

r

'\w+@\w+.\w+'

,

email

)

if

result2:

   

print

(

f

"Email {email} has a valid format"

)

else

:

   

print

(

f

"Email {email} has an invalid format"

)

Expected output:

手机号 138-1234-5678 格式正确
邮箱 test@example.com 格式正确

Code analysis:

Example 5: comparing match and fullmatch

Example

import

re

text

=

"hello world"

match only matches from the beginning

match_result

=

re

.

match

(

r

'hello'

,

text

)

fullmatch requires a complete match

fullmatch_result

=

re

.

fullmatch

(

r

'hello'

,

text

)

print

(

"match result:"

,

match_result.

group

(

)

if

match_result

else

None

)

print

(

"fullmatch result:"

,

fullmatch_result.

group

(

)

if

fullmatch_result

else

None

)

print

(

"-"

20

)

Try to match the entire string completely

text2

=

"hello"

match_result2

=

re

.

match

(

r

'hello'

,

text2

)

fullmatch_result2

=

re

.

fullmatch

(

r

'hello'

,

text2

)

print

(

"match result:"

,

match_result2.

group

(

)

if

match_result2

else

None

)

print

(

"fullmatch result:"

,

fullmatch_result2.

group

(

)

if

fullmatch_result2

else

None

)

Expected output:

match 结果: hello
fullmatch 结果: None
--------------------
match 结果: hello
fullmatch 结果: hello

Code analysis:

Example 6: using groups

Example

import

re

text

=

"2024-04-02"

Use groups to fully match a date

result

=

re

.

fullmatch

(

r

'(\d{4})-(\d{2})-(\d{2})'

,

text

)

if

result:

   

print

(

"Full match:"

,

result.

group

(

)

)

   

print

(

"Year:"

,

result.

group

(

1

)

)

   

print

(

"Month:"

,

result.

group

(

2

)

)

   

print

(

"Day:"

,

result.

group

(

3

)

)

Expected output:

完整匹配: 2024-04-02
年: 2024
月: 04
日: 02

Python re Module Python re Module