I have the text below :
text='apples and oranges apples and grapes apples and lemons'
and I want to use regular expressions to achieve something like below:
‘apples and oranges’
‘apples and lemons’
I tried this re.findall('apples and (oranges|lemons)',text) , but it doesn’t work.
Update: If the ‘oranges’ and ‘lemons’ were a list : new_list=['oranges','lemons'], how could I go to (?:’oranges’|’lemons’) without typing them again ?
Any ideas? Thanks.
re.findall(): If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group.Try this:
(?:...)is a non-capturing version of regular parentheses.