Basic Concepts
A regular expression, often called a pattern, is an expression that specifies a set of strings. To specify such sets of strings, rules are often more concise than lists of a set's members. For example, the set containing the three strings "Handel", "Händel", and "Haendel" can be specified by the pattern H(ä|ae?)ndel
(or alternatively, it is said that the pattern matches each of the three strings). In most formalisms, if there exists at least one regex that matches a particular set then there exist an infinite number of such expressions. Most formalisms provide the following operations to construct regular expressions.
- Boolean "or"
- A vertical bar separates alternatives. For example,
gray|grey
can match "gray" or "grey". - Grouping
- Parentheses are used to define the scope and precedence of the operators (among other uses). For example,
gray|grey
andgr(a|e)y
are equivalent patterns which both describe the set of "gray" and "grey". - Quantification
- A quantifier after a token (such as a character) or group specifies how often that preceding element is allowed to occur. The most common quantifiers are the question mark
?
, the asterisk*
(derived from the Kleene star), and the plus sign+
(Kleene cross).
-
?
The question mark indicates there is zero or one of the preceding element. For example, colou?r
matches both "color" and "colour".*
The asterisk indicates there is zero or more of the preceding element. For example, ab*c
matches "ac", "abc", "abbc", "abbbc", and so on.+
The plus sign indicates there is one or more of the preceding element. For example, ab+c
matches "abc", "abbc", "abbbc", and so on, but not "ac".
These constructions can be combined to form arbitrarily complex expressions, much like one can construct arithmetical expressions from numbers and the operations +, −, ×, and ÷. For example, H(ae?|ä)ndel
and H(a|ae|ä)ndel
are both valid patterns which match the same strings as the earlier example, H(ä|ae?)ndel
.
The precise syntax for regular expressions varies among tools and with context; more detail is given in the Syntax section.
Read more about this topic: Regular Expression
Famous quotes containing the words basic and/or concepts:
“The basic rule of human nature is that powerful people speak slowly and subservient people quicklybecause if they dont speak fast nobody will listen to them.”
—Michael Caine [Maurice Joseph Micklewhite] (b. 1933)
“Institutional psychiatry is a continuation of the Inquisition. All that has really changed is the vocabulary and the social style. The vocabulary conforms to the intellectual expectations of our age: it is a pseudo-medical jargon that parodies the concepts of science. The social style conforms to the political expectations of our age: it is a pseudo-liberal social movement that parodies the ideals of freedom and rationality.”
—Thomas Szasz (b. 1920)