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, Java java.util.regex). (applied hands-on: Text Processing with sed and tr, Lab 0)

🧱 Inductive definition β€” the expressions

  1. and are regular expressions.
  2. Every letter of the alphabet is a regular expression.
  3. If and are regular expressions, then so are: Β· Β· Β· .

🧭 Inductive definition β€” what they mean

ExpressionLanguage 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 unitCommon alternativeMeaning
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