regexxx match python code example
Example 1: regex findall
import re
# regex for finding mentions in a tweet
regex = r"(?<!RT\s)@\S+"
tweet = '@tony I am so over @got and @sarah is dead to me.'
# mentions = ['@tony', '@got', '@sarah']
mentions = re.findall(regex, tweet)
Example 2: python re
if pattern := re.search("[iI] am (.*)", "I am tired"):
print(f"Hi {pattern.group(1)}, I'm dad!")