Regular Expressions(RegEx) in Python: Key Functions and Examples

 Regular Expressions (RegEx) in Python :

A regular expression (RegEx) is a sequence of characters that defines a search pattern. It is used for pattern matching within strings, such as searching, replacing, or extracting information. Python provides the re module to work with regular expressions.


Key Functions in Python's re module:

import re  # Import the 're' module to work with regular expressions.

1. re.match() - Matches the pattern at the beginning of the string.

result = re.match(r'\d+', '123abc')  # Match one or more digits at the start of '123abc'
print(result.group()) #output : 123


 2. re.search() - Searches for the pattern anywhere in the string.

result = re.search(r'\d+', 'abc123def')  # Search for one or more digits anywhere in 'abc123def'
print(result.group()) #output : 123


3. re.findall() - Finds all occurrences of the pattern in the string.

result = re.findall(r'\d+', 'abc123def456')  # Find all occurrences of one or more digits in 'abc123def456'
print("Find all:", result)  # Output: Find all: ['123', '456']



4. re.sub() - Replaces occurrences of the pattern with a specified string.

result = re.sub(r'\d+', 'X', 'abc123def456')  # Replace all digits in 'abc123def456' with 'X'
print("Substitute:", result)  # Output: Substitute: abcXdefX



5. re.split() - Splits the string at occurrences of the pattern.

result = re.split(r'\d+', 'abc123def456')  # Split 'abc123def456' wherever digits are found
print("Split result:", result)  # Output: Split result: ['abc', 'def', '']



Example of special characters in a pattern:
\
`.`: Matches any character except a newline.
`^`: Matches the start of the string.
`$`: Matches the end of the string.
`[]`: Matches any character inside the brackets.
`|`: Acts as an OR operator in patterns.
`*`, `+`, `?`: Quantifiers for repetition (zero or more, one or more, zero or one).
 `()` groups patterns.



Program:

# Example of using a special character for validation:
email = "example@domain.com"
# Validating an email format using regular expression
pattern = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'  # Email regex pattern
if re.match(pattern, email):  # Check if the email matches the pattern
    print("Valid email address")
else:
    print("Invalid email address")


Post a Comment

0 Comments