YouTip LogoYouTip

Python Re Match

## Python `re.match()` Method The `re.match()` function in Python's `re` module is used to **match a regular expression pattern against the beginning of a string**. If the zero or more characters at the beginning of the string match the regular expression pattern, the function returns a corresponding match object. If no match is found, it returns `None`. --- ## Syntax and Parameters `re.match()` is a module-level function that can be called directly from the `re` module. ### Syntax ```python re.match(pattern, string, flags=0) ``` ### Parameter Description * **`pattern`**: * **Type**: String (`str`) or compiled pattern object. * **Description**: The regular expression pattern to be matched. * **`string`**: * **Type**: String (`str`) * **Description**: The target string to search within. * **`flags`**: * **Type**: Integer (`int`, optional) * **Description**: Regular expression modifiers (e.g., `re.IGNORECASE`, `re.MULTILINE`). Multiple flags can be combined using the bitwise OR operator `|`. ### Return Value * Returns a **Match Object** if the pattern matches at the beginning of the string. * Returns **`None`** if the pattern does not match the start of the string. > **Key Note**: `re.match()` only checks for a match at the **very beginning** of the string. If you want to search for a pattern anywhere within the string, use `re.search()` instead. --- ## Code Examples The following practical examples demonstrate how to use `re.match()` in different scenarios. ### Example 1: Basic Usage - Matching at the Beginning This example shows a successful match because the target string starts with the specified pattern. ```python import re text = "Python is a powerful programming language" # Match "Python" at the beginning of the string result = re.match(r'Python', text) if result: print("Match successful:", result.group()) print("Match position:", result.start(), "-", result.end()) ``` **Expected Output:** ```text Match successful: Python Match position: 0 - 6 ``` --- ### Example 2: Unsuccessful Match (Pattern Not at the Start) This example demonstrates that `re.match()` fails if the pattern exists in the string but not at the very beginning. ```python import re text = "I love Python" # Attempt to match "Python" at the beginning - this 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:** ```text Match failed: The string does not start with 'Python' ``` --- ### Example 3: Matching Patterns with Regular Expressions You can use standard regex metacharacters (like `\d` for digits) to match dynamic patterns at the start of a string. ```python import re text = "2024 is a special year" # Match a 4-digit year at the beginning of the string result = re.match(r'\d{4}', text) if result: print("Matched year:", result.group()) ``` **Expected Output:** ```text Matched year: 2024 ``` --- ### Example 4: Using Groups to Extract Data By using parentheses `()` in your regular expression, you can define capture groups and extract specific parts of the matched string. ```python import re text = "Hello World" # Match "Hello" followed by a space and "World", capturing both words in groups result = re.match(r'(Hello) (World)', text) if result: print("Full match:", result.group()) # Equivalent to result.group(0) print("Group 1:", result.group(1)) # Captures the first parenthesized group print("Group 2:", result.group(2)) # Captures the second parenthesized group ``` **Expected Output:** ```text Full match: Hello World Group 1: Hello Group 2: World ``` --- ## Key Considerations 1. **`re.match()` vs. `re.search()`**: * `re.match()` checks for a match **only at the beginning** of the string (index 0). * `re.search()` scans the **entire string** and returns the first location where the pattern matches. 2. **Handling `None`**: Always check if the result of `re.match()` is not `None` before calling methods like `.group()`, `.start()`, or `.end()`. Calling these methods on `None` will raise an `AttributeError`. 3. **Raw Strings**: It is highly recommended to use raw string notation (e.g., `r'\d+'`) for regular expression patterns to avoid accidental escaping of backslashes in Python.
← Python Re SplitPython Re Escape β†’