GetWiki
Regex
ARTICLE SUBJECTS
being →
database →
ethics →
fiction →
history →
internet →
language →
linux →
logic →
method →
news →
policy →
purpose →
religion →
science →
software →
truth →
unix →
wiki →
ARTICLE TYPES
essay →
feed →
help →
system →
wiki →
ARTICLE ORIGINS
critical →
forked →
imported →
original →
A regular expression (abbreviated as regexp or regex) is a string that describes a whole set of strings, according to certain syntax rules. These expressions are used by many text editors and utilities (especially in the Unix operating system) to search bodies of text for certain patterns and, for example, replace the found strings with a certain other string.
The origin of regular expressions lies in automata theory and formal language theory (both part of theoretical computer science). These fields study models of computation (automata) and ways to describe and classify formal languages. A formal language is nothing but a set of strings. In the 1940s, Warren McCulloch and Walter Pitts described the nervous system by modelling neurons as small simple automata. The mathematician, Stephen Kleene, later described these models using his mathematical notation called regular sets. Ken Thompson built this notation into the editor qed, then into the Unix editor ed and eventually into grep. Ever since that time, regular expressions have been widely used in Unix and Unixlike utilities such as: expr, awk, Emacs, vim, lex, and Perl. Most tools use an implementation of the regex library built by Henry Spencer.
Regular expressions correspond to the type 3 grammars of the Chomsky hierarchy and may be used to describe a regular language.
Regular expressions in Formal Language Theory
Regular expressions consist of constants and operators that denote sets of strings and operations over these sets, respectively. Given a finite alphabet Σ the following constants are defined: (empty set) &8709; denoting the set &8709;
 (empty string) &949; denoting the set {&949;}
 (literal character) a in &931; denoting the set {"a"}
 (concatenation) RS denoting the set { &945;&946;  &945; in R and &946; in S }. For example {"ab", "c"}{"d", "ef"} = {"abd", "abef", "cd", "cef"}.
 (set union) R U S denoting the set union of R and S.
 (Kleene star) R denoting the smallest superset of R that contains &949; and is closed under string concatenation. This is the set of all strings that can be made by concatenating zero or more strings in R. For example, {"ab", "c"} = {&949;, "ab", "c", "abab", "abc", "cab", "cc", "ababab", ... }.
To avoid brackets it is assumed that the Kleene star has the highest priority, then concatenation and then set union. If there is no ambiguity then brackets may be omitted. For example, (ab)c is written as abc and a U (b(c*)) can be written as a U bc*.
Sometimes the complement operator ~ is added; ~R denotes the set of all strings over Σ that are not in R. In that case the resulting operators form a Kleene algebra. The complement operator is redundant: it can always be expressed by only using the other operators.
Examples:
 a U b denotes {"a", &949;, "b", "bb", "bbb", ...}
 (a U b) denotes the set of all strings consisting of 'a's and 'b's, including the empty string
 b(ab) the same
 ab(c U &949;) denotes the set of strings starting with 'a', then zero or more 'b's and finally optionally a 'c'.
 ( bb U a(bb)aa U a(bb)(ab U ba)(bb)(ab U ba) ) denotes the set of all strings which contain an even number of 'b's and a number of 'a's divisible by three.
Regular expressions in this sense can express exactly the class of languages accepted by finite state automata: the regular languages. There is, however, a significant difference in compactness: some classes of regular languages can only be described by automata that grow exponentially in size, while the required regular expressions only grow linearly.
We can also study expressive power within the formalism. As the example shows, different regular expressions can express the same language: the formalism is redundant.
It is possible to write an algorithm which for two given regular expressions decides whether the described languages are equal  essentially, it reduces each expression to a minimal deterministic finite state automaton and determines whether they are isomorphic (equivalent).
To what extent can this redundancy be eliminated? Can we find an interesting subset of regular expressions that is still fully expressive? Kleene star and set union are obviously required, but perhaps we can restrict their use. This turns out to be a surprisingly difficult problem. As simple as the regular expressions are, it turns out there is no method to systematically rewrite them to some normal form. They are not finitely axiomatizable. So we have to resort to other methods. This leads to the star height problem.
Regular expression syntaxes
Traditional Unix regexps
The "basic" Unix regexp syntax is now defined as obsolete by POSIX, but is still widely used for the purposes of backwards compatibility. Most Unix utilities (grep, sed...) use it by default.In this syntax, most characters are treated as literals  they match only themselves ("a" matches "a", "abc" matches "abc", etc). The exceptions are called metacharacters:
.  Matches any single character 
[ ]  Matches a single character that is contained within the brackets  [abc] matches "a", "b", or "c". [az] matches any lowercase letter. 
[^ ]  Matches a single character that is not contained within the brackets  [^az] matches any single character that isn't a lowercase letter 
^  Matches the start of the line 
$  Matches the end of the line 
( )  Mark a part of the expression. What the enclosed expression matched to can be recalled by n where n is a digit from 1 to 9. 
n  Where n is a digit from 1 to 9; matches to the exact string what the expression enclosed in the n 'th left parenthesis and its paring right parenthesis has been matched to. This construct is theoretically irregular and has not adopted in the extended regular expression syntax. 
* 

{x,y}  Match the last "block" at least x and not more than y times.  "a{3,5}" matches "aaa", "aaaa" or "aaaaa". 
There is no representation of the set union operator in this syntax.
Examples:
".at" matches any threeletter word ending with "at"
"[hc]at" matches "hat" and "cat"
"[^b]at" matches any threeletter word ending with "at" and not beginning with 'b'.
"^[hc]at" matches "hat" and "cat" but only at the beginning of a line
"[hc]at$" matches "hat" and "cat" but only at the end of a line
"[hc]at" matches "hat" and "cat"
"[^b]at" matches any threeletter word ending with "at" and not beginning with 'b'.
"^[hc]at" matches "hat" and "cat" but only at the beginning of a line
"[hc]at$" matches "hat" and "cat" but only at the end of a line
POSIX modern (extended) regexps
The more modern "extended" regexp can often be used with modern Unix utilities by including the command line flag "E".POSIX extended regexps are similar in syntax to the traditional Unix regexp, with some exceptions. The following metacharacters are added:
+  Match the last "block" one or more times  "ba+" matches "ba", "baa", "baaa" and so on 
?  Match the last "block" zero or one times  "ba?" matches "b" or "ba" 
  The choice (or set union) operator: match either the expression before or the expression after the operator  "abcdef" matches "abc" or "def". 
Also, backslashes are removed: {...} becomes {...} and (...) becomes (...)
Examples:
"[hc]+at" matches with "hat", "cat", "hhat", "chat", "hcat", "ccat" et cetera
"[hc]?at" matches "hat", "cat" and "at"
"([cC]at  [dD]og)" matches "cat", "Cat", "dog" and "Dog"
Since the characters '(', ')', '[', ']', '.', '*', '?', '+', '^' and '$' are used as special symbols they have to be "escaped" somehow if they are meant literally. This is done by preceding them with '' which therefore also has to be "escaped" this way if meant literally.
"[hc]?at" matches "hat", "cat" and "at"
"([cC]at  [dD]og)" matches "cat", "Cat", "dog" and "Dog"
Examples:
"..(())" matches with the string "a.)"
Perl Compatible Regular Expressions (PCRE)
Perl has a much richer syntax than even the extended POSIX regexp. This syntax has also been used in other utilities and applications  exim, for example. Although still named "regular expressions", the Perl extensions give an expressive power that far exceeds the regular languages.This extra power comes at a cost. The worstcase complexity of matching a string against a Perl regular expression is exponential in the size of the input. That means the regular expression could take an extremely long time to process under just the right conditions of expression and input string, although it rarely happens in practice.
External links
 Regular expression information
 Syntax and Semantics of Regular Expressions by Xerox Research Centre Europe
 Regexp Syntax Summary, reference table for different styles of regular expressions
 Learning to Use Regular Expressions by David Mertz
 The Regex Coach, a very powerful interactive learning system to hone your regexp skills
 Mastering Regular Expressions book website
Some content adapted from the Wikinfo article "Regular expression" under the GNU Free Documentation License.
[ last updated: 10:08pm EDT  Tue, Apr 03 2007 ]
LATEST EDITS [ see all ]
GETWIKI 23 MAY 2022
The Illusion of Choice
Culture
Culture
GETWIKI 09 JUL 2019
Eastern Philosophy
History of Philosophy
History of Philosophy
GETWIKI 09 MAY 2016
GetMeta:About
GetWiki
GetWiki
GETWIKI 18 OCT 2015
M.R.M. Parrott
Biographies
Biographies
GETWIKI 20 AUG 2014
GetMeta:News
GetWiki
GetWiki
© 20072007, 20042023 M.R.M. PARROTT  ALL RIGHTS RESERVED