Advanced Patterns

Because you thought you knew Regex, but now you're in over your head...

Greedy vs. Lazy

When to use .* or .*? in your regex:

Example:

                import re

# Greedy
pattern = re.compile(r'\d+')
s = '123456'
match = pattern.match(s)
if match:
    print(match.group())

# Lazy
pattern = re.compile(r'\d+')
s = '123456'
match = pattern.search(s)
if match:
    print(match.group())
            

More info: More Greedy than Lazy

Still confused? Match all the things!