Learn regular expressions by seeing patterns, test cases, and match results side-by-side
Learn fundamental regex patterns including literal characters, wildcards, and basic quantifiers
The simplest regex pattern is a literal string. The pattern cat matches the exact sequence of characters "cat".
Key points:
The cat sat on the mat. I have a dog, not a cat. Concatenate these strings. CAT is not the same as cat.
The dot . is a wildcard that matches any single character except newline.
Pattern breakdown:
c - literal 'c'. - any single charactert - literal 't'This matches "cat", "cot", "cut", and even "c t" (with a space), but not "ct" (missing middle character) or "coat" (too many characters between c and t).
Source: Python re Documentation
cat cot cut c t ct coat
The star * quantifier means "zero or more" of the preceding character.
Pattern breakdown:
c - literal 'c'a* - zero or more 'a' characterst - literal 't'This matches "ct" (zero a's), "cat" (one a), "caat" (two a's), etc. It does not match "cbt" because the middle character must be 'a' if present.
ct cat caat caaaat cbt
The plus + quantifier means "one or more" of the preceding character.
Pattern breakdown:
c - literal 'c'a+ - one or more 'a' characterst - literal 't'Unlike *, this requires at least one 'a'. So "ct" does not match, but "cat", "caat", etc. do.
Source: Python Regex HOWTO
ct cat caat caaaat
The question mark ? quantifier means "zero or one" of the preceding character (makes it optional).
Pattern breakdown:
colou?r - matches both "color" and "colour"This is perfect for handling spelling variations between American and British English.
color colour colouur color vs colour