In Python, regular expressions (regex) are supported through the built-in re module.
The regular expression is a sequence of characters with specialized syntax that we can use to find or match characters or pattern matching within strings.
Here's a overview of regex in Python:
# import regular expression module using `re` module import re
# compile pattern pattern = re.compile(r'pattern value')
We can use the "r" prefix before the pattern string to treat it as a raw string, which helps to avoid accidental escape characters.
re.match(): The `match` function tries to match a pattern at the beginning of a string.
re.search(): The `search` function searches for the first occurrence of a pattern in a string.
re.findall(): The `findall` function returns all occurrences of a pattern in a string as a list of strings.
re.finditer(): This `finditer` function returns an iterator yielding match objects for all non-overlapping matches of a pattern in a string.
re.fullmatch(): The `fullmatch()` function is used to match the whole string with a regex pattern.
Searches for the first occurrence of a pattern in a string.
# import regular expression module using `re` module import re # compile pattern pattern = re.compile(r'single-line') stringValue = 'This is a single-line string' result = pattern.search(stringValue) print(result.group()) # Output: single-line newPattern = re.compile(r'new') checkForNonExistingValue = newPattern.search(stringValue) print(checkForNonExistingValue) # Output: None
# Another Way import re newPattern = r"Hi" text = "Hi, My name is Alice" search = re.search(newPattern, text) print(search.group()) # Output: Hi if search: print("Pattern Match found:", search.group()) # Pattern Match found: Hi else: print("No match")
Checks if the pattern matches at the beginning of the string.
# import regular expression module using `re` module import re # compile pattern pattern = re.compile(r'This') string = 'This is a single-line string' result = pattern.match(string) # Printing the result print(result.group()) # Output: This
# Another Way import re newPattern = r"Hi" text = "Hi, My name is Alice" match = re.match(newPattern, text) print(match.group()) # Output: Hi if match: print("Pattern Match found:", match.group()) # Pattern Match found: Hi else: print("No match")
Finds all occurrences of the pattern in the string and returns them as a list.
# import regular expression module using `re` module import re # compile pattern pattern = re.compile(r'e') string = 'This is a single-line string' # Apply findall function result = pattern.findall(string) # Printing the result print(result) # Output: ['e', 'e']
# Another Way import re newPattern = r"e" text = "Hi, My name is Alice" findall = re.findall(newPattern, text) print(findall) # Output: ['e', 'e'] if findall: print("Pattern Match found:", findall) # Pattern Match found: ['e', 'e'] else: print("No match")
Returns an iterator yielding match objects over all non-overlapping matches in the string.
# import regular expression module using `re` module import re # Defining regular expression pattern pattern = re.compile(r'is') string = 'This is a single-line string' matches = pattern.finditer(string) # Printing the matches using loop over iterator for match in matches: print("Match found at index:", match.start()) # Match found at index: 2 # Match found at index: 5
fullmatch() function is used to match the whole string with a regex pattern.
import re pattern = r"Hi, My Name is Alice" text = "Hi, My Name is Alice" matches = re.fullmatch(pattern, text) print("Match found:", matches.group()) # Output: Match found: Hi, My Name is Alice
After applying a regex method, we can access the match object properties like "group()", "start()", and "end()" to get the matched "text", "start index", and "end index" respectively.
# import regular expression module using `re` module import re # Defining regular expression pattern pattern = re.compile(r'single-line') #Declaring string value string = 'This is a single-line string' # Searching for the first occurrence result = pattern.search(string) print(result.group()) # Output: 'single-line' # Finding all occurrences result = pattern.findall(string) print(result) # Output: ['single-line'] # Matching at the beginning result = pattern.match(string) print(result) # Output: None
Regular expressions can be complex and powerful tools for string manipulation and pattern matching in Python.
Understanding the syntax and usage is essential for effective text processing.