Definition
A regular expression (regex) is a formal notation describing a set of strings (a regular language) — used to specify token patterns for the lexer.
Key Points — Operations
a|b): matches a OR bab): matches a followed by ba*): zero or more repetitions of aa+): one or more repetitionsa?): zero or one occurrencedigit → 0|1|...|9, num → digit+Example
letter (letter | digit)*digit+/* (any char)* */ (needs care to avoid greedy over-match)📌 CSS Frequency: Moderate-high — often a "write a regex for..." numeric-type question (e.g., regex for a valid email, identifier, or binary string divisible by 3).
Model Answer (short)
"A regular expression is built from union, concatenation, and Kleene closure over an alphabet, and it precisely specifies token patterns for a lexer. For example, the identifier pattern letter(letter|digit)* matches any string starting with a letter followed by letters or digits — exactly what a compiler needs to recognize variable names."