Python re.match() Method
re.match()is a Pythonremodule used formatching from the beginning of the stringpattern-matching function.
If the beginning of the string matches the specified regular expression, a match object is returned; otherwise, returnsNone。
Word Meaning: matchIt means to match and to conform.
Basic Syntax and Parameters
re.match()is a module-level function, called directly viarethe module call.
Syntax Format
re.match(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 be matched.
- flags:
- Type: integer (int, optional)
- Description: Regular expression flags.
Function Description
- Return Value: If the beginning of the string matches, returns a
matchmatch object; otherwise returnsNone。 - Note:
re.match()Only matches from the beginning of the string; it does not scan the middle part of the string.
Examples
Let's master through examplesre.match()the usage of.
Example 1: Basic Usage - Matching at the Beginning
Example
import re
text = "Python is a powerful programming language"
# Match "Python" from the beginning
result = re.match(r'Python', text)
if result:
print(Match successful:, result.group())
print(Match position:, result.start(), "-", result.end())
text = "Python is a powerful programming language"
# Match "Python" from the beginning
result = re.match(r'Python', text)
if result:
print(Match successful:, result.group())
print(Match position:, result.start(), "-", result.end())
Expected output:
匹配成功: Python 匹配位置: 0 - 6
Example 2: When the Beginning Does Not Match
Example
import re
text = I am Python
# Match "Python" from the beginning - will fail
result = re.match(r'Python', text)
if result:
print(Match successful:, result.group())
else:
print(Match failed: the string does not start with Python)
text = I am Python
# Match "Python" from the beginning - will fail
result = re.match(r'Python', text)
if result:
print(Match successful:, result.group())
else:
print(Match failed: the string does not start with Python)
Expected output:
匹配失败: 字符串开头不是Python
Example 3: Matching Content After the Beginning of the String
Example
import re
text = 2024 is a special year
# Match the year format (4 digits)
result = re.match(r'\d{4}', text)
if result:
print(Matched year:, result.group())
text = 2024 is a special year
# Match the year format (4 digits)
result = re.match(r'\d{4}', text)
if result:
print(Matched year:, result.group())
Expected output:
匹配到年份: 2024
Example 4: Using Groups to Extract Data
Example
import re
text = "Hello World"
# Match "Hello" followed by a space and "World"
result = re.match(r'(Hello) (World)', text)
if result:
print(Full match:, result.group())
print(Group 1:, result.group(1))
print(Group 2:, result.group(2))
text = "Hello World"
# Match "Hello" followed by a space and "World"
result = re.match(r'(Hello) (World)', text)
if result:
print(Full match:, result.group())
print(Group 1:, result.group(1))
print(Group 2:, result.group(2))
Expected output:
完整匹配: Hello World 第1组: Hello 第2组: World
Other Extensions
Python re Module