Home > Mobile >  python regex finding all the first starting words of a string that are in a list
python regex finding all the first starting words of a string that are in a list

Time:10-10

Provided a list of words I would like to get all the list of words of a string AT THE START OF THE STRING that belong to the list.

p=['horse','pig','cow']
f="cow horse this"
joinit = '\s*|'.join(p) 
rex = rf"^({joinit})(({joinit})*)?"
print(rex)
matches = re.search(rex,f)
matches

Explanation: I try to get the first word with ^ and then match any amount of all the other words.

This does not work because I expect the extraction of "cow horse" but I only get "cow"

Some idea?

CodePudding user response:

Try (regex101):

import re

p = ["horse", "pig", "cow"]
f = "cow horse this"

pat = "|".join(map(re.escape, p))
pat = re.compile(fr"(?:\s*(?:{pat})\b) ")

print(pat.match(f).group(0))

Prints:

cow horse
  • Related