Greedy/Non-Greedy Matching
Python Greedy and non-greedy matching explained with examples.
We'll cover the following...
There are times when you want to match a pattern only optionally! The ?
character flags the group that precedes it as an optional part of the pattern. For example, enter the following into the interactive shell:
Press + to interact
import reRegex = re.compile(r'(scientific )?programming')m1 = Regex.search('Learn programming')m2 = Regex.search('Learn scientific programming')print m1.group()print m2.group()
The output will be:
...