Regular expression

A regular expression, regex or regexp (sometimes called a rational expression) is a sequence of that define a search . Usually such patterns are used by s for "find" or "find and replace" operations on, or for input validation. It is a technique developed in and  theory.

The concept arose in the 1950s when the American mathematician formalized the description of a . The concept came into common use with text-processing utilities. Different for writing regular expressions have existed since the 1980s, one being the  standard and another, widely used, being the  syntax.

Regular expressions are used in s, search and replace dialogs of s and s, in text processing utilities such as and  and in. Many s provide regex capabilities either built-in or via.

Patterns
The phrase regular expressions, and consequently, regexes, is often used to mean the specific, standard textual syntax (distinct from the mathematical notation described below) for representing patterns for matching text. Each character in a regular expression (that is, each character in the string describing its pattern) is either a, having a special meaning, or a regular character that has a literal meaning. For example, in the regex, a is a literal character which matches just 'a', while '.' is a meta character that matches every character except a newline. Therefore, this regex matches, for example, 'a ', or 'ax', or 'a0'. Together, metacharacters and literal characters can be used to identify text of a given pattern, or process a number of instances of it. Pattern matches may vary from a precise equality to a very general similarity, as controlled by the metacharacters. For example,  is a very general pattern,   (match all lower case letters from 'a' to 'z') is less general and   is a precise pattern (matches just 'a'). The metacharacter syntax is designed specifically to represent prescribed targets in a concise and flexible way to direct the automation of of a variety of input data, in a form easy to type using a standard.

A very simple case of a regular expression in this syntax is to locate a word spelled two different ways in a, the regular expression  matches both "serialise" and "serialize". also achieve this, but are more limited in what they can pattern, as they have fewer metacharacters and a simple language-base.

The usual context of s is in similar names in a list of files, whereas regexes are usually employed in applications that pattern-match text strings in general. For example, the regex  matches excess whitespace at the beginning or end of a line. An advanced regular expression that matches any numeral is.

A regex processor translates a regular expression in the above syntax into an internal representation which can be executed and matched against a representing the text being searched in. One possible approach is the to construct a  (NFA), which is then  and the resulting  (DFA) is run on the target text string to recognize substrings that match the regular expression. The picture shows the NFA scheme  obtained from the regular expression , where s denotes a simpler regular expression in turn, which has already been  translated to the NFA N(s).

History
Regular expressions originated in 1951, when mathematician described s using his mathematical notation called regular events. These arose in, in the subfields of (models of computation) and the description and classification of s. Other early implementations of  include the  language, which did not use regular expressions, but instead its own pattern matching constructs.

Regular expressions entered popular use from 1968 in two uses: pattern matching in a text editor and lexical analysis in a compiler. Among the first appearances of regular expressions in program form was when built Kleene's notation into the editor  as a means to match patterns in s. For speed, Thompson implemented regular expression matching by  (JIT) to  code on the, an important early example of JIT compilation. He later added this capability to the Unix editor, which eventually led to the popular search tool 's use of regular expressions ("grep" is a word derived from the command for regular expression searching in the ed editor:  meaning "Global search for Regular Expression and Print matching lines"). Around the same time when Thompson developed QED, a group of researchers including implemented a tool based on regular expressions that is used for  in  design.

Many variations of these original forms of regular expressions were used in programs at  in the 1970s, including, , , , and , and in other programs such as. Regexes were subsequently adopted by a wide range of programs, with these early forms standardized in the standard in 1992.

In the 1980s the more complicated regexes arose in, which originally derived from a regex library written by (1986), who later wrote an implementation of Advanced Regular Expressions for. The Tcl library is a hybrid / implementation with improved performance characteristics. Software projects that have adopted Spencer's Tcl regular expression implementation include. Perl later expanded on Spencer's original library to add many new features. Part of the effort in the design of is to improve Perl's regex integration, and to increase their scope and capabilities to allow the definition of s. The result is a  called, which are used to define Perl 6 grammar as well as provide a tool to programmers in the language. These rules maintain existing features of Perl 5.x regexes, but also allow -style definition of a via sub-rules.

The use of regexes in structured information standards for document and database modeling started in the 1960s and expanded in the 1980s when industry standards like (precursored by ANSI "GCA 101-1983") consolidated. The kernel of the standards consists of regexes. Its use is evident in the element group syntax.

Starting in 1997, developed  (Perl Compatible Regular Expressions), which attempts to closely mimic Perl's regex functionality and is used by many modern tools including  and.

Today, regexes are widely supported in programming languages, text processing programs (particularly ), advanced text editors, and some other programs. Regex support is part of the of many programming languages, including  and, and is built into the syntax of others, including Perl and. Implementations of regex functionality is often called a regex engine, and a number of libraries are available for reuse.

Basic concepts
A regular expression, often called a pattern, is an expression used to specify a of strings required for a particular purpose. A simple way to specify a finite set of strings is to list its or members. However, there are often more concise ways to specify the desired set of strings. For example, the set containing the three strings "Handel", "Händel", and "Haendel" can be specified by the pattern ; we say that this pattern matches each of the three strings. In most s, if there exists at least one regular expression that matches a particular set then there exists an infinite number of other regular expressions that also match it—the specification is not unique. Most formalisms provide the following operations to construct regular expressions.


 * Boolean "or"
 * A separates alternatives. For example, grey can match "gray" or "grey".


 * Grouping
 * are used to define the scope and precedence of the (among other uses). For example,   and e)y are equivalent patterns which both describe the set of "gray" or "grey".


 * Quantification
 * A after a  (such as a character) or group specifies how often that a preceding element is allowed to occur. The most common quantifiers are the  , the    (derived from the ), and the.

The wildcard  matches any character. For example,  matches any string that contains an "a", then any other character and then a "b",   matches any string that contains an "a" and a "b" at some later point.
 * style="width:15px; vertical-align:top;" |
 * The question mark indicates zero or one occurrences of the preceding element. For example,  matches both "color" and "colour".
 * style="vertical-align:top;" |
 * The asterisk indicates zero or more occurrences of the preceding element. For example,  matches "ac", "abc", "abbc", "abbbc", and so on.
 * style="vertical-align:top;" |
 * The plus sign indicates one or more occurrences of the preceding element. For example,  matches "abc", "abbc", "abbbc", and so on, but not "ac".
 * The preceding item is matched exactly n times.
 * The preceding item is matched min or more times.
 * The preceding item is matched at least min times, but not more than max times.
 * }
 * Wildcard
 * The preceding item is matched min or more times.
 * The preceding item is matched at least min times, but not more than max times.
 * }
 * Wildcard
 * The preceding item is matched at least min times, but not more than max times.
 * }
 * Wildcard
 * Wildcard

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,  and ae are both valid patterns which match the same strings as the earlier example,.

The precise for regular expressions varies among tools and with context; more detail is given in the  section.

Formal language theory
Regular expressions describe s in. They have the same expressive power as s.

Formal definition
Regular expressions consist of constants, which denote sets of strings, and operator symbols, which denote operations over these sets. The following definition is standard, and found as such in most textbooks on formal language theory. Given a finite Σ, the following constants are defined as regular expressions:
 * (empty set) ∅ denoting the set ∅.
 * () ε denoting the set containing only the "empty" string, which has no characters at all.
 * ()  in Σ denoting the set containing only the character a.

Given regular expressions R and S, the following operations over them are defined to produce regular expressions:
 * () RS denotes the set of strings that can be obtained by concatenating a string in R and a string in S. For example, let R = {"ab", "c"}, and S = {"d", "ef"}. Then, RS = {"abd", "abef", "cd", "cef"}.
 * () R | S denotes the of sets described by R and S. For example, if R describes {"ab", "c"} and S describes {"ab", "d", "ef"}, expression R | S describes {"ab", "c", "d", "ef"}.
 * () R* denotes the smallest of the set described by R that contains ε and is  under string concatenation. This is the set of all strings that can be made by concatenating any finite number (including zero) of strings from the set described by R. For example, {"0","1"}* is the set of all finite s (including the empty string), and {"ab", "c"}* = {ε, "ab", "c", "abab", "abc", "cab", "cc", "ababab", "abcab", ... }.

To avoid parentheses it is assumed that the Kleene star has the highest priority, then concatenation and then alternation. If there is no ambiguity then parentheses may be omitted. For example,  can be written as , and   can be written as. Many textbooks use the symbols ∪, +, or ∨ for alternation instead of the vertical bar.

Examples:
 * denotes {ε, "a", "b", "bb", "bbb", ...}
 * denotes the set of all strings with no symbols other than "a" and "b", including the empty string: {ε, "a", "b", "aa", "ab", "ba", "bb", "aaa", ...}
 * denotes the set of strings starting with "a", then zero or more "b"s and finally optionally a "c": {"a", "ac", "ab", "abc", "abb", "abbc", ...}
 * denotes the set of binary numbers that are multiples of 3: { ε, "0", "00", "11", "000", "011", "110", "0000", "0011", "0110", "1001", "1100", "1111", "00000", ... }

Expressive power and compactness
The formal definition of regular expressions is minimal on purpose, and avoids defining  and  —these can can be expressed as follows:   = , and   =. Sometimes the operator is added, to give a generalized regular expression; here Rc matches all strings over Σ* that do not match R. In principle, the complement operator is redundant, because it doesn't grant any more expressive power. However, it can make a regular expression much more concise—eliminating all complement operators from a regular expression can cause a blow-up of its length.

Regular expressions in this sense can express the regular languages, exactly the class of languages accepted by. There is, however, a significant difference in compactness. Some classes of regular languages can only be described by deterministic finite automata whose size grows in the size of the shortest equivalent regular expressions. The standard example here is the languages Lk consisting of all strings over the alphabet {a,b} whose kth-from-last letter equals a. On one hand, a regular expression describing L4 is given by $$(a\mid b)^*a(a\mid b)(a\mid b)(a\mid b)$$.

Generalizing this pattern to Lk gives the expression: $$(a\mid b)^*a\underbrace{(a\mid b)(a\mid b)\cdots(a\mid b)}_{k-1\text{ times}}. \, $$

On the other hand, it is known that every deterministic finite automaton accepting the language Lk must have at least 2k states. Luckily, there is a simple mapping from regular expressions to the more general (NFAs) that does not lead to such a blowup in size; for this reason NFAs are often used as alternative representations of regular languages. NFAs are a simple variation of the type-3 of the.

In the opposite direction, there are many languages easily described by a DFA that are not easily described a regular expression. For instance, determining the validity of a given requires computing the modulus of the integer base 11, and can be easily implemented with an 11-state DFA. However, a regular expression to answer the same problem of divisibility by 11 is at least multiple megabytes in length.

Given a regular expression, computes an equivalent nondeterministic finite automaton. A conversion in the opposite direction is achieved by.

Finally, it is worth noting that many real-world "regular expression" engines implement features that cannot be described by the regular expressions in the sense of formal language theory; rather, they implement regexes. See for more on this.

Deciding equivalence of regular expressions
As seen in many of the examples above, there is more than one way to construct a regular expression to achieve the same results.

It is possible to write an that, for two given regular expressions, decides whether the described languages are equal; the algorithm reduces each expression to a, and determines whether they are  (equivalent).

Algebraic laws for regular expressions can be obtained using a method by Gischer which is best explained along an example: In order to check whether (X+Y)* and (X* Y*)* denote the same regular language, for all regular expressions X, Y, it is necessary and sufficient to check whether the particular regular expressions (a+b)* and (a* b*)* denote the same language over the alphabet Σ={a,b}. More generally, an equation E=F between regular-expression terms with variables holds if, and only if, its instantiation with different variables replaced by different symbol constants holds.

The redundancy can be eliminated by using and  to find an interesting subset of regular expressions that is still fully expressive, but perhaps their use can be restricted. This is a surprisingly difficult problem. As simple as the regular expressions are, there is no method to systematically rewrite them to some normal form. The lack of axiom in the past led to the. In 1991, axiomatized regular expressions as a, using equational and  axioms. Already in 1964, Redko had proved that no finite set of purely equational axioms can characterize the algebra of regular languages.

Syntax
A regex pattern matches a target string. The pattern is composed of a sequence of atoms. An atom is a single point within the regex pattern which it tries to match to the target string. The simplest atom is a literal, but grouping parts of the pattern to match an atom will require using  as metacharacters. Metacharacters help form: atoms; quantifiers telling how many atoms (and whether it is a or not); a logical OR character, which offers a set of alternatives, and a logical NOT character, which negates an atom's existence; and backreferences to refer to previous atoms of a completing pattern of atoms. A match is made, not when all the atoms of the string are matched, but rather when all the pattern atoms in the regex have matched. The idea is to make a small pattern of characters stand for a large number of possible strings, rather than compiling a large list of all the literal possibilities.

Depending on the regex processor there are about fourteen metacharacters, characters that may or may not have their character meaning, depending on context, or whether they are "escaped", i.e. preceded by an, in this case, the backslash. Modern and POSIX extended regexes use metacharacters more often than their literal meaning, so to avoid "backslash-osis" or it makes sense to have a metacharacter escape to a literal mode; but starting out, it makes more sense to have the four bracketing metacharacters   and   be primarily literal, and "escape" this usual meaning to become metacharacters. Common standards implement both. The usual metacharacters are  and. The usual characters that become metacharacters when escaped are  and.

Delimiters
When entering a regex in a programming language, they may be represented as a usual string literal, hence usually quoted; this is common in C, Java, and Python for instance, where the regex  is entered as. However, they are often written with slashes as s, as in  for the regex. This originates in, where  is the editor command for searching, and an expression   can be used to specify a range of lines (matching the pattern), which can be combined with other commands on either side, most famously   as in  ("global regex print"), which is included in most -based operating systems, such as  distributions. A similar convention is used in, where search and replace is given by  and patterns can be joined with a comma to specify a range of lines as in. This notation is particularly well known due to its use in, where it forms part of the syntax distinct from normal string literals. In some cases, such as sed and Perl, alternative delimiters can be used to avoid collision with contents, and to avoid having to escape occurrences of the delimiter character in the contents. For example, in sed the command  will replace a   with an , using commas as delimiters.

Standards
The  standard has three sets of compliance: BRE (Basic Regular Expressions), ERE (Extended Regular Expressions), and SRE (Simple Regular Expressions). SRE is, in favor of BRE, as both provide backward compatibility. The subsection below covering the character classes applies to both BRE and ERE.

BRE and ERE work together. ERE adds,  , and  , and it removes the need to escape the metacharacters   and  , which are required in BRE. Furthermore, as long as the POSIX standard syntax for regexes is adhered to, there can be, and often is, additional syntax to serve specific (yet POSIX compliant) applications. Although POSIX.2 leaves some implementation specifics undefined, BRE and ERE provide a "standard" which has since been adopted as the default syntax of many tools, where the choice of BRE or ERE modes is usually a supported option. For example, GNU grep has the following options: "grep -E" for ERE, and "grep -G" for BRE (the default), and "grep -P" for Perl regexes.

Perl regexes have become a de facto standard, having a rich and powerful set of atomic expressions. Perl has no "basic" or "extended" levels. As in POSIX EREs,  and   are treated as metacharacters unless escaped; other metacharacters are known to be literal or symbolic based on context alone. Additional functionality includes, , named capture groups, and patterns.

POSIX basic and extended
In the standard, Basic Regular Syntax (BRE) requires that the s   and   be designated   and , whereas Extended Regular Syntax (ERE) does not.

Examples:
 * matches any three-character string ending with "at", including "hat", "cat", and "bat".
 * matches "hat" and "cat".
 * matches all strings matched by  except "bat".
 * matches all strings matched by  other than "hat" and "cat".
 * matches "hat" and "cat", but only at the beginning of the string or line.
 * matches "hat" and "cat", but only at the end of the string or line.
 * matches any single character surrounded by "[" and "]" since the brackets are escaped, for example: "[a]" and "[b]".
 * matches s followed by zero or more characters, for example: "s" and "saw" and "seed".

POSIX extended
The meaning of metacharacters with a backslash is reversed for some characters in the POSIX Extended Regular Expression (ERE) syntax. With this syntax, a backslash causes the metacharacter to be treated as a literal character. So, for example,  is now   and   is now. Additionally, support is removed for  backreferences and the following metacharacters are added:

Examples:
 * matches "at", "hat", and "cat".
 * matches "at", "hat", "cat", "hhat", "chat", "hcat", "cchchat", and so on.
 * matches "hat", "cat", "hhat", "chat", "hcat", "cchchat", and so on, but not "at".
 * matches "cat" or "dog".

POSIX Extended Regular Expressions can often be used with modern Unix utilities by including the flag -E.

Character classes
The character class is the most basic regex concept after a literal match. It makes one small sequence of characters match a larger set of characters. For example,  could stand for the uppercase alphabet, and   could mean any digit. Character classes apply to both POSIX levels.

When specifying a range of characters, such as  (i.e. lowercase   to uppercase  ), the computer's locale settings determine the contents by the numeric ordering of the character encoding. They could store digits in that sequence, or the ordering could be abc…zABC…Z, or aAbBcC…zZ. So the POSIX standard defines a character class, which will be known by the regex processor installed. Those definitions are in the following table:

POSIX character classes can only be used within bracket expressions. For example,  matches the uppercase letters and lowercase "a" and "b".

An additional non-POSIX class understood by some tools is, which is usually defined as   plus underscore. This reflects the fact that in many programming languages these are the characters that may be used in identifiers. The editor further distinguishes word and word-head classes (using the notation   and  ) since in many programming languages the characters that can begin an identifier are not the same as those that can occur in other positions.

Note that what the POSIX regex standards call character classes are commonly referred to as POSIX character classes in other regex flavors which support them. With most other regex flavors, the term character class is used to describe what POSIX calls bracket expressions.

Perl and PCRE
Because of its expressive power and (relative) ease of reading, many other utilities and programming languages have adopted syntax similar to 's &mdash; for example,, , , , , , Microsoft's , and. Some languages and tools such as and  support multiple regex flavors. Perl-derivative regex implementations are not identical and usually implement a subset of features found in Perl 5.0, released in 1994. Perl sometimes does incorporate features initially found in other languages, for example, Perl 5.10 implements syntactic extensions originally developed in PCRE and Python.

Lazy matching
In Python and some other implementations (e.g. Java), the three common quantifiers (,  and  ) are  by default because they match as many characters as possible. The regex  (including the double-quotes) applied to the string

"Ganymede," he continued, "is the largest moon in the Solar System."

matches the entire line (because the entire line begins and ends with a double-quote) instead of matching only the first part,. The aforementioned quantifiers may, however, be made lazy or minimal or reluctant, matching as few characters as possible, by appending a question mark:  matches only.

However, the whole sentence can still be matched in some circumstances. The question-mark operator does not change the meaning of the dot operator, so this still can match the double-quotes in the input. A pattern like  will still match the whole input if this is the string:

"Ganymede," he continued, "is the largest moon in the Solar System." EOF

To ensure that the double-quotes cannot be part of the match, the dot has to be replaced, e. g. like this:  This will match a quoted text part without additional double-quotes in it.

Possessive matching
In Java, quantifiers may be made possessive by appending a plus sign, which disables backing off, even if doing so would allow the overall match to succeed: While the regex  applied to the string

"Ganymede," he continued, "is the largest moon in the Solar System."

matches the entire line, the regex  does, because   consumes the entire input, including the final. Thus, possessive quantifiers are most useful with negated character classes, e.g., which matches   when applied to the same string.

Possessive quantifiers are easier to implement than greedy and lazy quantifiers, and are typically more efficient at runtime.

Patterns for non-regular languages
Many features found in virtually all modern regular expression libraries provide an expressive power that far exceeds the s. For example, many implementations allow grouping subexpressions with parentheses and recalling the value they match in the same expression (). This means that, among other things, a pattern can match strings of repeated words like "papa" or "WikiWiki", called squares in formal language theory. The pattern for these strings is.

The language of squares is not regular, nor is it, due to the. However, with an unbounded number of backreferences, as supported by numerous modern tools, is still.

However, many tools, libraries, and engines that provide such constructions still use the term regular expression for their patterns. This has led to a nomenclature where the term regular expression has different meanings in and pattern matching. For this reason, some people have taken to using the term regex, regexp, or simply pattern to describe the latter. , author of the Perl programming language, writes in an essay about the design of Perl 6:

Implementations and running times
There are at least three different s that decide whether and how a given regex matches a string.

The oldest and fastest relies on a result in formal language theory that allows every (NFA) to be transformed into a  (DFA). The DFA can be constructed explicitly and then run on the resulting input string one symbol at a time. Constructing the DFA for a regular expression of size m has the time and memory cost of (2m), but it can be run on a string of size n in time O(n).

An alternative approach is to simulate the NFA directly, essentially building each DFA state on demand and then discarding it at the next step. This keeps the DFA implicit and avoids the exponential construction cost, but running cost rises to O(mn). The explicit approach is called the DFA algorithm and the implicit approach the NFA algorithm. Adding caching to the NFA algorithm is often called the "lazy DFA" algorithm, or just the DFA algorithm without making a distinction. These algorithms are fast, but using them for recalling grouped subexpressions, lazy quantification, and similar features is tricky.

The third algorithm is to match the pattern against the input string by. This algorithm is commonly called NFA, but this terminology can be confusing. Its running time can be exponential, which simple implementations exhibit when matching against expressions like aa)*b that contain both alternation and unbounded quantification and force the algorithm to consider an exponentially increasing number of sub-cases. This behavior can cause a security problem called (ReDoS).

Although backtracking implementations only give an exponential guarantee in the worst case, they provide much greater flexibility and expressive power. For example, any implementation which allows the use of backreferences, or implements the various extensions introduced by Perl, must include some kind of backtracking. Some implementations try to provide the best of both algorithms by first running a fast DFA algorithm, and revert to a potentially slower backtracking algorithm only when a backreference is encountered during the match.

Unicode
In theoretical terms, any token set can be matched by regular expressions as long as it is pre-defined. In terms of historical implementations, regexes were originally written to use characters as their token set though regex libraries have supported numerous other s. Many modern regex engines offer at least some support for. In most respects it makes no difference what the character set is, but some issues do arise when extending regexes to support Unicode.


 * Supported encoding. Some regex libraries expect to work on some particular encoding instead of on abstract Unicode characters. Many of these require the encoding, while others might expect, or . In contrast, Perl and Java are agnostic on encodings, instead operating on decoded characters internally.
 * Supported Unicode range. Many regex engines support only the, that is, the characters which can be encoded with only 16 bits. Currently (as of 2016) only a few regex engines (e.g., Perl's and Java's) can handle the full 21-bit Unicode range.
 * Extending ASCII-oriented constructs to Unicode. For example, in ASCII-based implementations, character ranges of the form  are valid wherever x and y have s in the range [0x00,0x7F] and codepoint(x) ≤ codepoint(y). The natural extension of such character ranges to Unicode would simply change the requirement that the endpoints lie in [0x00,0x7F] to the requirement that they lie in [0x0000,0x10FFFF]. However, in practice this is often not the case. Some implementations, such as that of, do not allow character ranges to cross Unicode blocks. A range like [0x61,0x7F] is valid since both endpoints fall within the Basic Latin block, as is [0x0530,0x0560] since both endpoints fall within the Armenian block, but a range like [0x0061,0x0532] is invalid since it includes multiple Unicode blocks. Other engines, such as that of the  editor, allow block-crossing but the character values must not be more than 256 apart.
 * Case insensitivity. Some case-insensitivity flags affect only the ASCII characters. Other flags affect all characters. Some engines have two different flags, one for ASCII, the other for Unicode. Exactly which characters belong to the POSIX classes also varies.
 * Cousins of case insensitivity. As ASCII has case distinction, case insensitivity became a logical feature in text searching. Unicode introduced alphabetic scripts without case like . For these, is not applicable. For scripts like Chinese, another distinction seems logical: between traditional and simplified. In Arabic scripts, insensitivity to  may be desired. In Japanese, insensitivity between  and  is sometimes useful.
 * Normalization. Unicode has . Like old typewriters, plain letters can be followed by one or more non-spacing symbols (usually diacritics like accent marks) to form a single printing character, but also provides precomposed characters, i.e. characters that already include one or more combining characters. A sequence of a character + combining character should be matched with the identical single precomposed character. The process of standardizing sequences of characters + combining characters is called normalization.
 * New control codes. Unicode introduced amongst others, s and text direction markers. These codes might have to be dealt with in a special way.
 * Introduction of character classes for Unicode blocks, scripts, and numerous other character properties. Block properties are much less useful than script properties, because a block can have code points from several different scripts, and a script can have code points from several different blocks. In and the  library, properties of the form   or   match characters in block X and   or   matches code points not in that block. Similarly, ,  , or   matches any character in the Armenian script. In general,   matches any character with either the binary property X or the general category X. For example,  ,  , or   matches any uppercase letter. Binary properties that are not general categories include  ,  ,  , and  . Examples of non-binary properties are  ,  , and.

Uses
Regexes are useful in a wide variety of tasks, and more generally, where the data need not be textual. Common applications include, (especially ), , simple , the production of  systems, and many other tasks.

While regexes would be useful on Internet s, processing them across the entire database could consume excessive computer resources depending on the complexity and design of the regex. Although in many cases system administrators can run regex-based queries internally, most search engines do not offer regex support to the public. Notable exceptions:,. Google Code Search has been shut down as of January 2012. It used a trigram index to speed queries.

Examples
The specific syntax rules vary depending on the specific implementation,, or in use. Additionally, the functionality of regex implementations can vary between s.

Because regexes can be difficult to both explain and understand without examples, interactive websites for testing regexes are a useful resource for learning regexes by experimentation. This section provides a basic description of some of the properties of regexes by way of illustration.

The following conventions are used in the examples.

metacharacter(s) ;; the metacharacters column specifies the regex syntax being demonstrated =~ m//          ;; indicates a regex match operation in Perl =~ s///         ;; indicates a regex substitution operation in Perl

Also worth noting is that these regexes are all Perl-like syntax. Standard regular expressions are different.

Unless otherwise indicated, the following examples conform to the programming language, release 5.8.8, January 31, 2006. This means that other implementations may lack support for some parts of the syntax shown here (e.g. basic vs. extended regex,  vs. , or lack of   instead of   ).

The syntax and conventions used in these examples coincide with that of other programming environments as well.

Induction
Regular expressions can often be created ("induced" or "learned") based on a set of example strings. This is known as the, and is part of the general problem of in. Formally, given examples of strings in a regular language, and perhaps also given examples of strings not in that regular language, it is possible to induce a grammar for the language, i.e., a regular expression that generates that language. Not all regular languages can be induced in this way (see ), but many can. For example, the set of examples {1, 10, 100}, and negative set (of counterexamples) {11, 1001, 101, 0} can be used to induce the regular expression 1⋅0* (1 followed by zero or more 0s).