← Back

Regular Expressions

Regular Expressions are strings over an alphabet Σ\Sigma that can be obtained as follows:

  1. ∅\emptyset is a regular expression
  2. ϵ\epsilon is a regular expression
  3. Every element a∈Σa \in \Sigma is a regular expression
  4. If α,β\alpha, \beta are regular expressions, then so is αβ\alpha\beta
  5. If α,β\alpha, \beta are regular expressions, then so is α∪β\alpha \cup \beta
  6. If α\alpha are regular expressions, then so is α∗\alpha^{*}
  7. α\alpha are regular expressions, then so is α+\alpha^{+}
  8. If α\alpha are regular expressions, then so is (α)(\alpha)

Example:

If Σ={a,b}\Sigma = \lbrace{a, b\rbrace}, the following are regular expressions:

  • ∅\emptyset
  • ϵ\epsilon
  • aa
  • (a∪b)∗(a \cup b)^{*}
  • abba∪ϵabba \cup \epsilon

Regular Expressions Define Languages

Semantic interpretation: the language L(α\alpha) expressed by a regular expression α\alpha:

  1. L(∅)=∅L(\emptyset) = \emptyset
  2. L(ϵ)={ϵ}L(\epsilon) = \lbrace{\epsilon\rbrace}
  3. L(c)={c}, where c∈ΣL(c) = \lbrace{c\rbrace},\ \text{where} \ c \in \Sigma
  4. L(αβ)=L(α)L(β)L(\alpha \beta) = L(\alpha) L(\beta)
  5. L(α∪β)=L(α)∪L(β)L(\alpha \cup \beta) = L(\alpha) \cup L(\beta)
  6. L(α∗)=L(α)∗L(\alpha^{*}) = L(\alpha)^{*}
  7. L(α+)L(\alpha^{+})
    • If L(a)L(a) is equal to ∅\emptyset, then L(a+)L(a+) is also equal to ∅\emptyset. Otherwise L(a+)L(a+) is the language that is formed by concatenating together one or more strings drawn from L(a)L(a)
  8. L((α))=L(α)L((\alpha)) = L(\alpha)

This is the way we create the set representations (the Regular Language) of the Regular Expression.

Examples:

L(a∗b∗)=L(a∗)L(b∗)=L(a)∗L(b)∗={a}∗{b}∗={anbm∣n,m≥0}L(a^{*}b^{*}) = L(a^{*})L(b^{*}) = L(a)^{*}L(b)^{*} = \lbrace{a\rbrace}^{*}\lbrace{b\rbrace}^{*} = \lbrace{a^{n}b^{m} | n, m \ge 0\rbrace}

L={w∈{a,b}∗ : ∣w∣ is even}={{aa}∪{ab}∪{ba}∪{bb}}∗L = \lbrace{w \in \lbrace{a, b\rbrace}^{*} \ : \ |w| \ \text{is even}\rbrace} = \lbrace{\lbrace{aa\rbrace} \cup \lbrace{ab\rbrace} \cup \lbrace{ba\rbrace} \cup \lbrace{bb\rbrace}\rbrace}^{*}

Operator Precedence in Regular Expressions

So a∗∪b∗≠(a∪b)∗a^{*} \cup b^{*} \neq (a \cup b)^{*} and (ab)∗≠a∗b∗(ab)^{*} \neq a^{*}b^{*}

Sometimes it ISN'T possible to make a Regular Expression to represent a Language:

Structural Induction

Finite State Machines and Regular Expressions define the same class of languages. This means that the class of languages that can be defined with regular expressions is EXACTLY the class of regular languages.

So by something called Thompson's Construction we can build and NDFSM.

Every NDFSM that we can build using Thompson's Construction has a starting state with no incoming edges and ONE accepting state that has no outgoing edges

Building Blocks

Here are the Building Blocks that we can use to build the NDFSM

Basic : L(∅) and L(a) where a∈ΣL(\emptyset) \ \text{and} \ L(a) \ \text{where} \ a \in \Sigma

Concatenation: α=βγ→ NDFSM for L(α)\alpha = \beta \gamma \rightarrow \ \text{NDFSM for} \ L(\alpha)

Union (Or): α=β∪γ→ NDFSM for L(α)\alpha = \beta \cup \gamma \rightarrow \ \text{NDFSM for} \ L(\alpha)

Kleene * (Star): α=β∗→ NDFSM for L(α)\alpha = \beta^{*} \rightarrow \ \text{NDFSM for} \ L(\alpha)

Example of Regular Expression to NDFSM using Thompson's Construction:

FSM To Regular Expression

Basically, take any state, and remove it from the FSM. Then you update the transition function/relation from the state on the left to the state on the right so that it is a regular expression

Example:

Formal Steps:

  1. Create a new initial state and a new, unique accepting state, neither of which is part of a loop.
BeforeAfter
  1. Remove states and arcs and replace with arcs labelled with larger and larger regular expressions.
  • Remove State 3
BeforeAfter

Explanation:

  • Remove State 2
BeforeAfter

Explanation:

  • Remove State 1
BeforeAfter

Some Extra/Real World Regular Expressions

SyntaxNameDescription
abcabcConcatenationMatches aa, then bb, then cc where aa, bb, and cc are any regexs
a∥b∥ca \| b \| cUnion (Or)Matches aa or bb or cc where aa, bb, and cc are any regexs
a⋆a^{\star}Kleene StarMatches 0 or more a′sa's where aa is any regex
Finish this later

Simplifying Regular Expressions

Regex's describe sets:

  • Union is commutative: α∪β=β∪α\alpha \cup \beta = \beta \cup \alpha
  • Union is associative: (α∪β)∪γ=α∪(β∪γ)(\alpha \cup \beta) \cup \gamma = \alpha \cup (\beta \cup \gamma)
  • ∅\emptyset is the identity for Union: α∪∅=∅∪α=α\alpha \cup \emptyset = \emptyset \cup \alpha = \alpha
  • Union is idempotent: α∪α=α\alpha \cup \alpha = \alpha

Concatenation:

  • Concatenation is associative: (αβ)γ=α(βγ)(\alpha\beta)\gamma = \alpha(\beta\gamma)
  • ϵ\epsilon is the identity for Concatenation: αϵ=ϵα=α\alpha \epsilon = \epsilon \alpha = \alpha
  • ∅\emptyset is the zero for concatenation: α∅=∅α=∅\alpha \emptyset = \emptyset \alpha = \emptyset

Concatenation distributes over Union:

  • (α∪β)γ=(αγ)∪(βγ)(\alpha \cup \beta) \gamma = (\alpha \gamma) \cup (\beta \gamma)
  • γ(α∪β)=(γα)∪(γβ)\gamma(\alpha \cup \beta) = (\gamma\alpha) \cup (\gamma\beta)

Kleene Star:

  • ∅⋆=ϵ\emptyset^{\star} = \epsilon
  • ϵ⋆=ϵ\epsilon^{\star} = \epsilon
  • (α⋆)⋆=α⋆(\alpha^{\star})^{\star} = \alpha^{\star}
  • α⋆α⋆=α⋆\alpha^{\star}\alpha^{\star} = \alpha^{\star}
  • (α∪β)⋆=(α⋆β⋆)⋆(\alpha \cup \beta)^{\star} = (\alpha^{\star}\beta^{\star})^{\star}

Pattern Matching

Any file that contains the pattern

L(Σ⋆ abcabb Σ⋆)L(\Sigma^{\star} \ \text{abcabb} \ \Sigma^{\star}) -> NFA -> DFA -> Minimize -> gets the minimal DFSM on 43

Pattern Searching

Any file that ends with the pattern

L(Σ⋆ abcabb)L(\Sigma^{\star} \ \text{abcabb}) -> NFA -> DFA -> Minimize -> gets the minimal DFSM on 44