regular expression (original) (raw)

While variations abound, fundamentally a regular expression consists of the following pieces:

Note that while these rules are not immediately in regularPlanetmathPlanetmathPlanetmathPlanetmathPlanetmathPlanetmath form, they can be transformed so that they are.

Formally, let S={∅,∪,*,(,)} and Σ an alphabet disjoint from S. Consider the languagePlanetmathPlanetmath L⁢(Σ) over Σ∪S specified below

    1. ∅∈L⁢(Σ),
    1. a∈L⁢(Σ) for each a∈Σ,
    1. if u∈L⁢(Σ), then u*∈L⁢(Σ),
    1. if u1,u2∈L⁢(Σ), then (u1∪u2) and (u1⁢u2) are both in L⁢(Σ), and
    1. among all languages over Σ∪S satisfying conditions 1-4, L⁢(Σ) is the smallest.

Then any element u∈L⁢(Σ) is called a regular expression over Σ.

Here is an example of a regular expression that specifies a grammarMathworldPlanetmath that generates the binary representation of all multiples of 3 (and only multiples of 3).

S ::= A⁢B
A ::= C⁢D
B ::= 0B|λ
C ::= 0C|λ
D ::= 1⁢E⁢𝟷
E ::= FE|λ
F ::= 0⁢G⁢𝟶
G ::= 1G|λ

A little further work is required to transform this grammar into an acceptable form for regular grammars, but it can be shown that this grammar (and any grammar specified by a regular expression) is equivalentMathworldPlanetmathPlanetmathPlanetmathPlanetmathPlanetmath to some regular grammar.

One can understand the language described by a regular expression in another way, by viewing the regular expression operators as shorthand for various set-theoretic operationsMathworldPlanetmath. Formally, the language L⁢(u) over Σ associated with a regular expression u over Σ is inductively defined as follows:

A language L over Σ is regular iff there is a regular expression u over Σ such that L=L⁢(u).

With this interpretationMathworldPlanetmathPlanetmath, it is quite straightforward to design a non-deterministic finite automaton that recognizes the language described by a regular expression. Of course, for computer implementations, one must transform this into a deterministic finite automaton, but there are various algorithmsMathworldPlanetmath for doing this efficiently. This process, production of a non-deterministic automaton and conversion to an equivalent deterministic automaton is approximately what is done in software packages implementing regular expression searching. In fact, most such packages implement operations impossible for a finite automaton, such as requiring a later part of the string to be the same as a previous part (the language {An⁢B⁢An⁢ for ⁢n≥0} is not regular but can be matched by most “regular expression” software; such capabilities are called “extended regular expressions”. None of these systems are powerful enough to recognize the language of balanced parentheses.

Regular expressions have many applications. Quite often they are used for powerful string matching and substitution features in many text editors and programming languages.