Regex Syntax Fundamentals
Regular expressions are built from a small set of primitives that combine into powerful patterns. Character classes let you match sets of characters: [a-z] matches any lowercase letter, [0-9] any digit, and shorthand classes like \d (digit), \w (word character), and \s (whitespace) cover the most common cases. Negated classes (\D, \W, \S) match everything except those characters.
Quantifiers control repetition: * means zero or more, + means one or more, ? means zero or one, and {n,m} specifies an exact range. By default these are greedy — they match as much as possible. Appending ? makes them lazy (e.g., .*?), matching the shortest possible string.
Anchors assert positions rather than characters: ^ matches the start of a line, $ the end, and \b a word boundary. Groups created with ( ) capture matched text for back-references or extraction, while non-capturing groups (?: ) group without capturing. Alternation with | acts as a logical OR — cat|dog matches either word. Test these building blocks live in the pattern field above.