Skip to content

Regex

re — Python docs

re.match vs re.search

  • match at the beginning of the string, or match the entire string
    • faster
  • Otherwise use search
  • both return a re.Match object
>>> m = re.match(r"(.*)BC(.*)", 'ABC-DE')
>>> m.group(2)
'-DE'

Always use raw (regex) strings

  • so that it doesn't escape "C:\Programs\nathan"
  • \n is a newline 😱
  • r"C:\Programs\nathan"
    • is what we expect 😄
  • need to escape ^ and $ and other chars though 😞
# need to escape $
>>> re.search(r"$100", "$100") == None
True
>>> re.search(r"\$100", "$100")
<re.Match object; span=(0, 4), match='$100'>

Readable Regex

Why regex is hard?

Every character is a statement

Like trying to write/read minified JS

Verbose mode

Min 64 (t=3868)

you can leave comments too!

def is_valid_uuid(uuid: str) -> bool:
    return bool(re.match(r"""
        ^
        [a-f\d]{8}  # 8 hex digits
        -
        [a-f\d]{4}  # 4 hex digits
        -
        [a-f\d]{4}  # 4 hex digits
        -
        [a-f\d]{4}  # 4 hex digits
        -
        [a-f\d]{12} # 12 hex digits
        $
    """, uuid, re.IGNORECASE | re.VERBOSE))

Using f-strings & r-strings with regex

https://death.andgravity.com/f-re


Last update: 2023-04-24