python list find all regex code example
Example 1: array search with regex python
import re
mylist = ["dog", "cat", "wildcat", "thundercat", "cow", "hooo"]
r = re.compile(".*cat")
newlist = list(filter(r.match, mylist)) # Read Note
print(newlist)
Example 2: 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)