RIPAL: Responsive and Intuitive Parsing for the Analysis of Language

Pages

Examples of complex regular expressions

Background

You've now seen the fundamental operators used in building regular expressions. Now let's look at the more complex types of regular expressions that can be built up using those operators.

Using an operator multiple times

Operators can be used multiple times in the same expression.

Example

Σ = {a, b, c, d, e}

R = a ∪ b ∪ c ∪ d ∪ e

This regular expression recognizes the strings:

  1. a
  2. b
  3. c
  4. d
  5. e

Example

Σ = {a, b, c, d, e}

R = abcde

This regular expression recognizes the string abcde.

Note

While this is also true for the Kleene closure operator, I'll omit an example because, for any regular expression R, R** = R*.

Using multiple operators

More complex regular expressions can be built up using multiple operators.

Example

Σ = {a, b, c}

R = ab ∪ c*

This regular expression recognizes the strings:

  1. ab
  2. ε
  3. c
  4. cc
  5. ccc
  6. ...

Example

Σ = {a, b, c}

R = a(b ∪ c*)

This regular expresion recognizes the strings:

  1. ab
  2. a
  3. ac
  4. acc
  5. accc
  6. ...

Going deep with parentheses

Since there is no maximum depth to expressions in parentheses, any order of operations can be applied deep down into regular expressions.

Example

Σ = {a, b, c, d, e, f}

R = a(b ∪ c(d(e ∪ f)*)*)

This regular expression recognizes the strings:

  1. ab
  2. ac
  3. acd
  4. acde
  5. acdf
  6. acdee
  7. acdef
  8. acdeee
  9. acdede
  10. ...

Note

Not every string has been enumerated here. The above examples simply illustrate some of the strings that belong to the specified language.


GitHub Repository: https://github.com/bprollinson/ripal

Copyright © 2017 Brendan Rollinson-Lorimer