a, 1, @).. : Matches any character except a newline.^ : Matches the start of the string.$ : Matches the end of the string.* : Matches 0 or more repetitions.+ : Matches 1 or more repetitions.? : Matches 0 or 1 repetition.{} : Matches a specific number of repetitions.Code Example:
re Modulere.search(pattern, string) : Searches for the pattern within the string.re.match(pattern, string) : Matches the pattern at the start of the string.re.findall(pattern, string) : Returns a list of all matches.re.finditer(pattern, string) : Returns an iterator yielding match objects.re.sub(pattern, repl, string) : Replaces the matches with repl.# re.search example
pattern = r'Python'
text = "I am learning Python programming."
match = re.search(pattern, text)
if match:
print(match.group()) # Output: 'Python'
# re.match example
pattern = r'I am'
text = "I am learning Python."
match = re.match(pattern, text)
if match:
print(match.group()) # Output: 'I am'
# re.findall example
pattern = r'\d+'
text = "There are 3 cats, 4 dogs, and 5 birds."
matches = re.findall(pattern, text)
print(matches) # Output: ['3', '4', '5']
# re.sub example
pattern = r'(\d+)'
text = "There are 3 cats, 4 dogs, and 5 birds."
new_text = re.sub(pattern, r'\1 animals', text)
print(new_text) # Output: 'There are 3 animals cats, 4 animals dogs, and 5 animals birds.'pattern = r'(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2}),(\d+) - (\w+): (.+)'
log = "2024-06-15 14:23:45,123 - INFO: Server started."
match = re.match(pattern, log)
if match:
date, time, ms, level, message = match.groups()
print(f"Date: {date}, Time: {time}, Milliseconds: {ms}, Level: {level}, Message: {message}")
# Output: Date: 2024-06-15, Time: 14:23:45, Milliseconds: 123, Level: INFO, Message: Server started.