(Nov-10-2020, 06:53 PM)Gribouillis Wrote: You could use catpuring groups
import re text = """ 23 24 10.00 to 11.00 - with a yellow overcoat with brown buttons and green rims. 25 26 12.00 to 13.00 - with short 27 shredded jeans with holes. 28 """ result = re.compile(r'(?P<A>\d+[.]\d+ to \d+[.]\d+ - \D+)\d+(?P<B>\D+)') matches = result.finditer(text) for match in matches: print(match.group('A') + match.group('B'))
Output:10.00 to 11.00 - with a yellow overcoat with brown buttons and green rims. 12.00 to 13.00 - with short shredded jeans with holes.
Thank you. That works really well and now I know something about groups, which led me to learn that we can use the 'either or' condition within REs using ' | '. I changed the input text slightly (added line 23) to test whether I could also extract the sentence starting with 'At 9.00 -' using the same RE. However, this doesn't do the trick and I can't figure out what I'm doing wrong.
import re text = """ 23 At 9.00 - people playing banjos wearing fancy clothes 24 10.00 to 11.00 - with a yellow overcoat with brown buttons and green rims. 25 26 12.00 to 13.00 - with short 27 shredded jeans with holes. 28 """ result = re.compile(r'((\w+ \d+[.]d+ - \D+)|(?P<A>\d+[.]\d+ to \d+[.]\d+ - \D+)\d+(?P<B>\D+))') matches = result.finditer(text) for match in matches: print(match.group('A') + match.group('B'))
Output:10.00 to 11.00 - with a
yellow overcoat with brown buttons
and green rims.
12.00 to 13.00 - with short
shredded jeans with holes.
Any suggestions? Thank you!