Regular language is a language which can be represented by a regular expression and thus every string in the language can be accepted by the corresponding deterministic finite automaton. Note: Regular Language should not be confused with Regular Expressions. For question regarding pattern matching within strings, use the [regex] tag instead.
Given an alphabet (finite set of symbols) Σ, a language is a set of all sequences of such symbols in that alphabet. A language is a regular language exactly when it can be expressed in terms of a (formal) regular expression and the membership of any string can be decided by a finite-state machine.
Regular languages belong to the highest hierarchy of the Chomsky Hierarchy, and are also called Type-3 grammars. They are above the Type-2 context-free languages which are recognized by pushdown automata, which are above the Type-1 context-sensitive languages recognized by linear bounded automata, and above the Type-0 recursively enumerable languages which can be recognized by Turing Machines. All regular languages are context-free, context-sensitive, and recursively enumerable. Formal regular expressions can be converted to deterministic finite state machines and to non deterministic finite machines and still represent the same regular language.
Please do not confuse this with regex. Most regex engines are far more expressive than formal regular expressions, finite state machines, and can represent non-regular languages.
Construction of a Regular Language
The set of all regular languages over a given alphabet Σ can be produced exactly by this process:
- The empty language
{}
, rejecting all strings. - The language containing only the empty string
ε
- All languages containing only a single symbol
s ∈ Σ
. - Every language created by the union, concatenation, or kleene-star of regular languages. Suppose
v
andw
are strings of a regular languageA
andB
respectively:- The union
(v|w)
is also regular. It accepts languages that are in any ofA
orB
. - The concatenation
vw
is also regular. - The kleene-star
v*
is also regular. It means any copies of strings inA
concatenated, including 0.
- The union
Examples and Nonexamples of Regular Languages
Given a simple alphabet
Σ = {0, 1}
, where|
represents union,*
represents kleene-star, these formal regular expressions all represent represents a regular language:- The regular expression
"0"
,"1"
,"(0|1)"
,"01"
,"11"
,"0*"
are all regular. - The regular expression
"(0(0|1)*1)"
, representing all binary strings beginning with 0 and ending with 1, is regular. - Given a regular expression
R
, the language"R+"
and"R?"
all represent a regular language, whereas+
represents one or more, and?
represents zero or one. Namely,"R+"
is equivalent to"RR*"
, and"R?"
is equivalent to"(R|ε)"
. - Given a regular expression
R
, the language"R{m,n}"
is regular for all naturalm,n
, where{m,n}
represents "fromm
copies ton
copies". This is because it also involves union and concatenation:"R{1,3}"
is expanded to"(R|RR|RRR)"
.
- The regular expression
Given an alphabet used by regex engines, usually an ASCII or Unicode alphabet containing all ASCII or Unicode characters respectively:
- The regex
/^.+$/
is regular. It includes all non-empty sequences of any character. - The regex
/^#[A-Za-z]{1,3}[0-9]{2,4}$/
represents a regular language, consisting all strings which being with a hashtag, then one to three ASCII letters, followed by two to four decimal digits. - The regex
/^([\d][\w])*$/
represents a regular language. It consists all strings which alternate digit characters and word characters. The shorthand\d
and\w
are examples of union.
- The regex
Many regex engines are much more expressive than regular languages. Backreferences can cause a regex to represent a non-regular language, and consequently they cannot be decided by a finite state machine.
- The regex
"(.+)\1"
represents an irregular language. Involving a backreference capturing the first group.+
, it accepts all the sequences of uppercase Latin letters repeated exactly twice. They are called squares in formal language theory."ABCABC"
,"1234.1234."
are accepted"ABCAB"
,"1234567891234567890"
are rejected.
- The regex
Further Reading
- regex: Short for regular expression. Many regex engines nowadays are far more expressive than formal regular expressions and finite-state machines.
- finite-state-machine: They are equivalent in expressiveness to formal regular expressions. They represent exactly the regular languages. Acronyms include FSM.
- dfa: Deterministic finite automaton. nfa Nondeterministic finite automaton.
- Both are equivalent in expressiveness.
- context-free-grammar, context-sensitive-grammar: Tags referring to lower levels of the Chomsky hierarchy
- Wikipedia, includes explanation about squares and irregular regexes
- What is a regular language?