Regular Expressions
Context: FIT2014_MOC Β· a finite notation for (often infinite) languages over Β· builds on Formal Languages (Alphabets, Words, Languages) Β· Assignment 1 material (regex + finite automata) Companion: Finding Regular Expressions β the description expression skill.
Quick Revision
- π― Objective: define languages by pattern rather than by listing β built inductively from , and letters using concatenation, union and Kleene star .
- β‘ Key Constraint: binds tightest β : the first is followed by any number of s, the second repeats the block .
π Why they exist
- Pattern matching β find URLs in logs, valid variable names, dates, numbers in mixed text.
- Everywhere in tools β editors (vi, emacs), filters (grep, sed, awk), lexical-analyser generators (lex, flex, JFlex), compiler generators (yacc, bison), and languages (Python
re, Perl, Javajava.util.regex). (applied hands-on: Text Processing with sed and tr, Lab 0)
π§± Inductive definition β the expressions
- and are regular expressions.
- Every letter of the alphabet is a regular expression.
- If and are regular expressions, then so are: Β· Β· Β· .
π§ Inductive definition β what they mean
| Expression | Language represented |
|---|---|
| the empty language | |
| β just the empty word | |
| (a word) | β exactly that one word |
| same language as (grouping only) | |
| β concatenation | |
| β union / alternatives | |
(writing for the language of and for the language of )
- Regular language β any language describable by a regular expression.
- Matched β a word is matched by if it belongs to βs language.
β The three operations
- Concatenation β if matches and matches , then matches .
- Union (alternatives) β describes .
- Grouping β describes .
- Kleene star β zero or more repetitions:
- Star unfolds as a union of powers β
π³ Structure (parse tree)
- Expressions have structure β parses as a concatenation of and , each decomposing further.
- Why it matters β identifying the last operation used to build the expression is the key to both reading and writing regexes.
βοΈ Alternative notations (tool-dependent)
| This unit | Common alternative | Meaning |
|---|---|---|
| alternatives | ||
[0-9] | character class | |
| letters to | [a-z] | range |
| one or more | ||
| optional |
- β Tools differ β in whether and carry these meanings, how parentheses/vertical bars are escaped, whether
.means βany non-newline characterβ, and how newlines are handled. The unitβs notation is the mathematical one.
β οΈ Common Mistakes
- π‘ β the star applies to the immediately preceding expression; group explicitly when you mean the block.
- π‘ includes zero copies β always contains ; if you need at least one, write (i.e. ).
- π‘ vs β represents the language with no words; represents , which has one. (same trap as in Formal Languages (Alphabets, Words, Languages))
- π‘ Not every language is regular β PALINDROME is now settled: it is not regular, proved by the pumping lemma (see Proving a Language Non-Regular). DOUBLEWORD is likewise non-regular, though the unit has not yet proved it.
π§ Active Recall
Distinguish the languages of , and .
Answer
- Short answer: (one , then any number of s); (repeats of the block); (any number of s then any number of s).
- Why: Scope of the star β binds to the immediately preceding expression, so grouping decides whether a block or a single letter repeats; concatenating two starred expressions gives an ordered pair of runs.
What does it mean for a language to be regular, and why is the inductive definition needed?
Answer
- Short answer: a language is regular iff some regular expression describes it; the word is matched if it lies in that language.
- Why: Finite description of infinite sets β the inductive rules (base cases , letters; closure under concatenation, , ) generate every regular expression from finitely many rules, and the parallel inductive semantics assigns each one a language β so infinite languages get finite descriptions.