Skip to content

Regex

https://www.stefanjudis.com/today-i-learned/the-dot-in-regexp-doesnt-match-all-characters-in-javascript/

Why are regex so hard?

Every character is a statement

Like trying to write/read minified JS

re.match vs re.search

  • match at the beginning of the string, or match the entire string
    • faster
  • Otherwise use search
  • both return a match object
>>> m = re.match("\w+", """something some other thing""")
>>> m
<re.Match object; span=(0, 9), match='something'>

Readable Regex

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 😞
>>> re.search(r"$100", "$100") == None
True
>>> re.search(r"\$100", "$100")
<re.Match object; span=(0, 4), match='$100'>

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: 2022-11-04