...

/

Regular Expressions

Regular Expressions

Learn how to use regular expressions to describe regular languages.

Regular expressions are a convenient notation for representing strings that match simple text patterns. The expression aacba^*ac^*b^* illustrates two of the four regular expression operations, namely concatenation (via juxtaposition) and Kleene star. The others appear in the following recursive definition.

Formal definition

  1. These are regular expressions:

    a) ϕ\phi, representing the empty language/set, {}\{ \}

    b) λ\lambda, representing the one-string language/set, {λ}\{\lambda\}

    c) cc, for each character, cc in the alphabet Σ\Sigma, representing the language/set {c}\{c\}

  2. For regular expressions r,r1,r2r, r_1, r_2, the following are also regular expressions:

    a) r1+r2r_1 + r_2 (union)

    b) r1r2r_1r_2 (concatenation)

    c) rr^* (Kleene star)

    d) (r)(r) (grouping)

Note: Some authors (and most programming environments) use the notation r1r2r_1 \mid r_2 for union.

Each regular expression represents a language, which is the set of all strings matching its pattern. Formally, we denote the language associated with a regular expression, rr, as L(r)L(r), so L(ϕ)={}L(\phi)=\{ \}, L(λ)={λ}L(\lambda)=\{\lambda\}, and L(c)={c}L(c)=\{c\} for each symbol cc from the alphabet in use. The following expressions also hold:

  • L(r1+r2)=L(r1)L(r2)L(r_1+r_2)=L(r_1)\cup L(r_2)
  • L(r1r2)=L(r1)L(r2)L(r_1r_2)=L(r_1)L(r_2)
...