python version: Python 3.10.4 (main, Mar 31 2022, 08:41:55) [GCC 7.5.0] on linux
re version: 2.2.1
I want to get all occurrences of a regex's group between two specific words.
First, the different expressions my program will encounter (... means this in a vaster text):
"...For colts, geldings and fillies of...""...For horses, geldings and mares of...""...For colts and geldings of...""...For colts and fillies of...""...For horses and geldings of...""...For colts of...""...For fillies of...""...For horses of...""...For mares of..."
The finality of the program is to get all the mentions of "colts", "geldings", "fillies", "horses","mares" between the words "For" and "of". Concretely I want 3 groups if there is 3 mentions, 2 groups for two mentions, and 1 group for one mention.
len(re.search(a_regex_pattern,"...For colts, geldings and fillies of...").groups())
>>> 3 # 3 groups
re.search(a_regex_pattern,"...For colts, geldings and fillies of...").groups()
>>> ['colts','geldings','fillies']
Where I am stuck is to find the right a_regex_pattern to do it.
I tried it:
a_regex_expression = "For.*?(colts|geldings|fillies){1,3}.*?of"
re.search(a_regex_pattern,"...For colts, geldings and fillies of...").groups()
>>> ['fillies']
Other tries are worse. How would you do it ?