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:
“Scientific reason, with its strict conscience, its lack of prejudice, and its determination to question every result again the moment it might lead to the least intellectual advantage, does in an area of secondary interest what we ought to be doing with the basic questions of life.”
—Robert Musil (18801942)
“During our twenties...we act toward the new adulthood the way sociologists tell us new waves of immigrants acted on becoming Americans: we adopt the host cultures values in an exaggerated and rigid fashion until we can rethink them and make them our own. Our idea of what adults are and what were supposed to be is composed of outdated childhood concepts brought forward.”
—Roger Gould (20th century)