9.7. Pattern Matching
There are three separate approaches to pattern matching provided byPostgres Pro: the traditionalSQLLIKE
operator, the more recentSIMILAR TO
operator (added in SQL:1999), andPOSIX-style regular expressions. Aside from the basic“does this string match this pattern?” operators, functions are available to extract or replace matching substrings and to split a string at matching locations.
Caution
Searches usingSIMILAR TO
patterns have the same security hazards, sinceSIMILAR TO
provides many of the same capabilities asPOSIX-style regular expressions.
string
LIKEpattern
[ESCAPEescape-character
]string
NOT LIKEpattern
[ESCAPEescape-character
]
TheLIKE
expression returns true if thestring
matches the suppliedpattern
. (As expected, theNOT LIKE
expression returns false ifLIKE
returns true, and vice versa. An equivalent expression isNOT (
.)string
LIKEpattern
)
Ifpattern
does not contain percent signs or underscores, then the pattern only represents the string itself; in that caseLIKE
acts like the equals operator. An underscore (_
) inpattern
stands for (matches) any single character; a percent sign (%
) matches any sequence of zero or more characters.
Some examples:
'abc' LIKE 'abc'true'abc' LIKE 'a%'true'abc' LIKE '_b_'true'abc' LIKE 'c'false
LIKE
pattern matching always covers the entire string. Therefore, if it's desired to match a sequence anywhere within a string, the pattern must start and end with a percent sign.
To match a literal underscore or percent sign without matching other characters, the respective character inpattern
must be preceded by the escape character. The default escape character is the backslash but a different one can be selected by using theESCAPE
clause. To match the escape character itself, write two escape characters.
Note
If you havestandard_conforming_strings turned off, any backslashes you write in literal string constants will need to be doubled. SeeSection 4.1.2.1 for more information.
It's also possible to select no escape character by writingESCAPE ''
. This effectively disables the escape mechanism, which makes it impossible to turn off the special meaning of underscore and percent signs in the pattern.
The key wordILIKE
can be used instead ofLIKE
to make the match case-insensitive according to the active locale. This is not in theSQL standard but is aPostgres Pro extension.
string
SIMILAR TOpattern
[ESCAPEescape-character
]string
NOT SIMILAR TOpattern
[ESCAPEescape-character
]
TheSIMILAR TO
operator returns true or false depending on whether its pattern matches the given string. It is similar toLIKE
, except that it interprets the pattern using the SQL standard's definition of a regular expression. SQL regular expressions are a curious cross betweenLIKE
notation and common regular expression notation.
LikeLIKE
, theSIMILAR TO
operator succeeds only if its pattern matches the entire string; this is unlike common regular expression behavior where the pattern can match any part of the string. Also likeLIKE
,SIMILAR TO
uses_
and%
as wildcard characters denoting any single character and any string, respectively (these are comparable to.
and.*
in POSIX regular expressions).
In addition to these facilities borrowed fromLIKE
,SIMILAR TO
supports these pattern-matching metacharacters borrowed from POSIX regular expressions:
|
denotes alternation (either of two alternatives).*
denotes repetition of the previous item zero or more times.+
denotes repetition of the previous item one or more times.?
denotes repetition of the previous item zero or one time.{
m
}
denotes repetition of the previous item exactlym
times.{
m
,}
denotes repetition of the previous itemm
or more times.{
m
,
n
}
denotes repetition of the previous item at leastm
and not more thann
times.Parentheses
()
can be used to group items into a single logical item.A bracket expression
[...]
specifies a character class, just as in POSIX regular expressions.
Notice that the period (.
) is not a metacharacter forSIMILAR TO
.
As withLIKE
, a backslash disables the special meaning of any of these metacharacters; or a different escape character can be specified withESCAPE
.
Some examples:
'abc' SIMILAR TO 'abc'true'abc' SIMILAR TO 'a'false'abc' SIMILAR TO '%(b|d)%'true'abc' SIMILAR TO '(b|c)%'false
Thesubstring
function with three parameters,substring(
, provides extraction of a substring that matches an SQL regular expression pattern. As withstring
frompattern
forescape-character
)SIMILAR TO
, the specified pattern must match the entire data string, or else the function fails and returns null. To indicate the part of the pattern that should be returned on success, the pattern must contain two occurrences of the escape character followed by a double quote ("
). The text matching the portion of the pattern between these markers is returned.
Some examples, with#"
delimiting the return string:
substring('foobar' from '%#"o_b#"%' for '#')oobsubstring('foobar' from '#"o_b#"%' for '#')NULL
9.7.3. POSIX Regular Expressions
Table 9.12 lists the available operators for pattern matching using POSIX regular expressions.
Table 9.12. Regular Expression Match Operators
Operator | Description | Example |
---|---|---|
~ | Matches regular expression, case sensitive | 'thomas' ~ '.*thomas.*' |
~* | Matches regular expression, case insensitive | 'thomas' ~* '.*Thomas.*' |
!~ | Does not match regular expression, case sensitive | 'thomas' !~ '.*Thomas.*' |
!~* | Does not match regular expression, case insensitive | 'thomas' !~* '.*vadim.*' |
'abc' ~ 'abc'true'abc' ~ '^a'true'abc' ~ '(b|d)'true'abc' ~ '^(b|c)'false
ThePOSIX pattern language is described in much greater detail below.
substring('foobar' from 'o.b')oobsubstring('foobar' from 'o(.)b')o
Theregexp_replace
function provides substitution of new text for substrings that match POSIX regular expression patterns. It has the syntaxregexp_replace
(source
,pattern
,replacement
[,flags
]). Thesource
string is returned unchanged if there is no match to thepattern
. If there is a match, thesource
string is returned with thereplacement
string substituted for the matching substring. Thereplacement
string can contain\
n
, wheren
is 1 through 9, to indicate that the source substring matching then
'th parenthesized subexpression of the pattern should be inserted, and it can contain\&
to indicate that the substring matching the entire pattern should be inserted. Write\\
if you need to put a literal backslash in the replacement text. Theflags
parameter is an optional text string containing zero or more single-letter flags that change the function's behavior. Flagi
specifies case-insensitive matching, while flagg
specifies replacement of each matching substring rather than only the first one. Supported flags (though notg
) are described inTable 9.20.
Some examples:
regexp_replace('foobarbaz', 'b..', 'X')fooXbazregexp_replace('foobarbaz', 'b..', 'X', 'g')fooXXregexp_replace('foobarbaz', 'b(..)', 'X\1Y', 'g')fooXarYXazY
Theregexp_matches
function returns a text array of all of the captured substrings resulting from matching a POSIX regular expression pattern. It has the syntaxregexp_matches
(string
,pattern
[,flags
]). The function can return no rows, one row, or multiple rows (see theg
flag below). If thepattern
does not match, the function returns no rows. If the pattern contains no parenthesized subexpressions, then each row returned is a single-element text array containing the substring matching the whole pattern. If the pattern contains parenthesized subexpressions, the function returns a text array whosen
'th element is the substring matching then
'th parenthesized subexpression of the pattern (not counting“non-capturing” parentheses; see below for details). Theflags
parameter is an optional text string containing zero or more single-letter flags that change the function's behavior. Flagg
causes the function to find each match in the string, not only the first one, and return a row for each such match. Supported flags (though notg
) are described inTable 9.20.
Some examples:
SELECT regexp_matches('foobarbequebaz', '(bar)(beque)'); regexp_matches ---------------- {bar,beque}(1 row)SELECT regexp_matches('foobarbequebazilbarfbonk', '(b[^b]+)(b[^b]+)', 'g'); regexp_matches ---------------- {bar,beque} {bazil,barf}(2 rows)SELECT regexp_matches('foobarbequebaz', 'barbeque'); regexp_matches ---------------- {barbeque}(1 row)
It is possible to forceregexp_matches()
to always return one row by using a sub-select; this is particularly useful in aSELECT
target list when you want all rows returned, even non-matching ones:
SELECT col1, (SELECT regexp_matches(col2, '(bar)(beque)')) FROM tab;
Theregexp_split_to_table
function splits a string using a POSIX regular expression pattern as a delimiter. It has the syntaxregexp_split_to_table
(string
,pattern
[,flags
]). If there is no match to thepattern
, the function returns thestring
. If there is at least one match, for each match it returns the text from the end of the last match (or the beginning of the string) to the beginning of the match. When there are no more matches, it returns the text from the end of the last match to the end of the string. Theflags
parameter is an optional text string containing zero or more single-letter flags that change the function's behavior.regexp_split_to_table
supports the flags described inTable 9.20.
Theregexp_split_to_array
function behaves the same asregexp_split_to_table
, except thatregexp_split_to_array
returns its result as an array oftext
. It has the syntaxregexp_split_to_array
(string
,pattern
[,flags
]). The parameters are the same as forregexp_split_to_table
.
Some examples:
SELECT foo FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', '\s+') AS foo; foo ------- the quick brown fox jumps over the lazy dog (9 rows)SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', '\s+'); regexp_split_to_array ----------------------------------------------- {the,quick,brown,fox,jumps,over,the,lazy,dog}(1 row)SELECT foo FROM regexp_split_to_table('the quick brown fox', '\s*') AS foo; foo ----- t h e q u i c k b r o w n f o x (16 rows)
As the last example demonstrates, the regexp split functions ignore zero-length matches that occur at the start or end of the string or immediately after a previous match. This is contrary to the strict definition of regexp matching that is implemented byregexp_matches
, but is usually the most convenient behavior in practice. Other software systems such as Perl use similar definitions.
9.7.3.1. Regular Expression Details
Postgres Pro's regular expressions are implemented using a software package written by Henry Spencer. Much of the description of regular expressions below is copied verbatim from his manual.
Regular expressions (REs), as defined inPOSIX 1003.2, come in two forms:extendedREs orEREs (roughly those ofegrep
), andbasicREs orBREs (roughly those ofed
).Postgres Pro supports both forms, and also implements some extensions that are not in the POSIX standard, but have become widely used due to their availability in programming languages such as Perl and Tcl.REs using these non-POSIX extensions are calledadvancedREs orAREs in this documentation. AREs are almost an exact superset of EREs, but BREs have several notational incompatibilities (as well as being much more limited). We first describe the ARE and ERE forms, noting features that apply only to AREs, and then describe how BREs differ.
Note
Postgres Pro always initially presumes that a regular expression follows the ARE rules. However, the more limited ERE or BRE rules can be chosen by prepending anembedded option to the RE pattern, as described inSection 9.7.3.4. This can be useful for compatibility with applications that expect exactly thePOSIX 1003.2 rules.
A quantified atom is anatom possibly followed by a singlequantifier. Without a quantifier, it matches a match for the atom. With a quantifier, it can match some number of matches of the atom. Anatom can be any of the possibilities shown inTable 9.13. The possible quantifiers and their meanings are shown inTable 9.14.
Aconstraint matches an empty string, but matches only when specific conditions are met. A constraint can be used where an atom could be used, except it cannot be followed by a quantifier. The simple constraints are shown inTable 9.15; some more constraints are described later.
Table 9.13. Regular Expression Atoms
Atom | Description |
---|---|
( re ) | (wherere is any regular expression) matches a match forre , with the match noted for possible reporting |
(?: re ) | as above, but the match is not noted for reporting (a“non-capturing” set of parentheses) (AREs only) |
. | matches any single character |
[ chars ] | abracket expression, matching any one of thechars (seeSection 9.7.3.2 for more detail) |
\ k | (wherek is a non-alphanumeric character) matches that character taken as an ordinary character, e.g.,\\ matches a backslash character |
\ c | wherec is alphanumeric (possibly followed by other characters) is anescape, seeSection 9.7.3.3 (AREs only; in EREs and BREs, this matchesc ) |
{ | when followed by a character other than a digit, matches the left-brace character{ ; when followed by a digit, it is the beginning of abound (see below) |
x | wherex is a single character with no other significance, matches that character |
An RE cannot end with a backslash (\
).
Note
If you havestandard_conforming_strings turned off, any backslashes you write in literal string constants will need to be doubled. SeeSection 4.1.2.1 for more information.
Table 9.14. Regular Expression Quantifiers
Quantifier | Matches |
---|---|
* | a sequence of 0 or more matches of the atom |
+ | a sequence of 1 or more matches of the atom |
? | a sequence of 0 or 1 matches of the atom |
{ m } | a sequence of exactlym matches of the atom |
{ m ,} | a sequence ofm or more matches of the atom |
{ m , n } | a sequence ofm throughn (inclusive) matches of the atom;m cannot exceedn |
*? | non-greedy version of* |
+? | non-greedy version of+ |
?? | non-greedy version of? |
{ m }? | non-greedy version of{ m } |
{ m ,}? | non-greedy version of{ m ,} |
{ m , n }? | non-greedy version of{ m , n } |
The forms using{
...
}
are known asbounds. The numbersm
andn
within a bound are unsigned decimal integers with permissible values from 0 to 255 inclusive.
Non-greedy quantifiers (available in AREs only) match the same possibilities as their corresponding normal (greedy) counterparts, but prefer the smallest number rather than the largest number of matches. SeeSection 9.7.3.5 for more detail.
Note
A quantifier cannot immediately follow another quantifier, e.g.,**
is invalid. A quantifier cannot begin an expression or subexpression or follow^
or|
.
Table 9.15. Regular Expression Constraints
Constraint | Description |
---|---|
^ | matches at the beginning of the string |
$ | matches at the end of the string |
(?= re ) | positive lookahead matches at any point where a substring matchingre begins (AREs only) |
(?! re ) | negative lookahead matches at any point where no substring matchingre begins (AREs only) |
Lookahead constraints cannot containback references (seeSection 9.7.3.3), and all parentheses within them are considered non-capturing.
9.7.3.2. Bracket Expressions
Abracket expression is a list of characters enclosed in[]
. It normally matches any single character from the list (but see below). If the list begins with^
, it matches any single characternot from the rest of the list. If two characters in the list are separated by-
, this is shorthand for the full range of characters between those two (inclusive) in the collating sequence, e.g.,[0-9]
inASCII matches any decimal digit. It is illegal for two ranges to share an endpoint, e.g.,a-c-e
. Ranges are very collating-sequence-dependent, so portable programs should avoid relying on them.
There are two special cases of bracket expressions: the bracket expressions[[:<:]]
and[[:>:]]
are constraints, matching empty strings at the beginning and end of a word respectively. A word is defined as a sequence of word characters that is neither preceded nor followed by word characters. A word character is analnum
character (as defined byctype) or an underscore. This is an extension, compatible with but not specified byPOSIX 1003.2, and should be used with caution in software intended to be portable to other systems. The constraint escapes described below are usually preferable; they are no more standard, but are easier to type.
Character-entry escapes exist to make it easier to specify non-printing and other inconvenient characters in REs. They are shown inTable 9.16.
Class-shorthand escapes provide shorthands for certain commonly-used character classes. They are shown inTable 9.17.
Aconstraint escape is a constraint, matching the empty string if specific conditions are met, written as an escape. They are shown inTable 9.18.
Aback reference (\
n
) matches the same string matched by the previous parenthesized subexpression specified by the numbern
(seeTable 9.19). For example,([bc])\1
matchesbb
orcc
but notbc
orcb
. The subexpression must entirely precede the back reference in the RE. Subexpressions are numbered in the order of their leading parentheses. Non-capturing parentheses do not define subexpressions.
Table 9.16. Regular Expression Character-entry Escapes
Escape | Description |
---|---|
\a | alert (bell) character, as in C |
\b | backspace, as in C |
\B | synonym for backslash (\ ) to help reduce the need for backslash doubling |
\c X | (whereX is any character) the character whose low-order 5 bits are the same as those ofX , and whose other bits are all zero |
\e | the character whose collating-sequence name isESC , or failing that, the character with octal value033 |
\f | form feed, as in C |
\n | newline, as in C |
\r | carriage return, as in C |
\t | horizontal tab, as in C |
\u wxyz | (wherewxyz is exactly four hexadecimal digits) the character whose hexadecimal value is0x wxyz |
\U stuvwxyz | (wherestuvwxyz is exactly eight hexadecimal digits) the character whose hexadecimal value is0x stuvwxyz |
\v | vertical tab, as in C |
\x hhh | (wherehhh is any sequence of hexadecimal digits) the character whose hexadecimal value is0x hhh (a single character no matter how many hexadecimal digits are used) |
\0 | the character whose value is0 (the null byte) |
\ xy | (wherexy is exactly two octal digits, and is not aback reference) the character whose octal value is0 xy |
\ xyz | (wherexyz is exactly three octal digits, and is not aback reference) the character whose octal value is0 xyz |
Hexadecimal digits are0
-9
,a
-f
, andA
-F
. Octal digits are0
-7
.
Numeric character-entry escapes specifying values outside the ASCII range (0-127) have meanings dependent on the database encoding. When the encoding is UTF-8, escape values are equivalent to Unicode code points, for example\u1234
means the characterU+1234
. For other multibyte encodings, character-entry escapes usually just specify the concatenation of the byte values for the character. If the escape value does not correspond to any legal character in the database encoding, no error will be raised, but it will never match any data.
The character-entry escapes are always taken as ordinary characters. For example,\135
is]
in ASCII, but\135
does not terminate a bracket expression.
Table 9.17. Regular Expression Class-shorthand Escapes
Escape | Description |
---|---|
\d | [[:digit:]] |
\s | [[:space:]] |
\w | [[:alnum:]_] (note underscore is included) |
\D | [^[:digit:]] |
\S | [^[:space:]] |
\W | [^[:alnum:]_] (note underscore is included) |
Within bracket expressions,\d
,\s
, and\w
lose their outer brackets, and\D
,\S
, and\W
are illegal. (So, for example,[a-c\d]
is equivalent to[a-c[:digit:]]
. Also,[a-c\D]
, which is equivalent to[a-c^[:digit:]]
, is illegal.)
Table 9.18. Regular Expression Constraint Escapes
Escape | Description |
---|---|
\A | matches only at the beginning of the string (seeSection 9.7.3.5 for how this differs from^ ) |
\m | matches only at the beginning of a word |
\M | matches only at the end of a word |
\y | matches only at the beginning or end of a word |
\Y | matches only at a point that is not the beginning or end of a word |
\Z | matches only at the end of the string (seeSection 9.7.3.5 for how this differs from$ ) |
A word is defined as in the specification of[[:<:]]
and[[:>:]]
above. Constraint escapes are illegal within bracket expressions.
Table 9.19. Regular Expression Back References
Escape | Description |
---|---|
\ m | (wherem is a nonzero digit) a back reference to them 'th subexpression |
\ mnn | (wherem is a nonzero digit, andnn is some more digits, and the decimal valuemnn is not greater than the number of closing capturing parentheses seen so far) a back reference to themnn 'th subexpression |
Note
There is an inherent ambiguity between octal character-entry escapes and back references, which is resolved by the following heuristics, as hinted at above. A leading zero always indicates an octal escape. A single non-zero digit, not followed by another digit, is always taken as a back reference. A multi-digit sequence not starting with a zero is taken as a back reference if it comes after a suitable subexpression (i.e., the number is in the legal range for a back reference), and otherwise is taken as octal.
9.7.3.4. Regular Expression Metasyntax
In addition to the main syntax described above, there are some special forms and miscellaneous syntactic facilities available.
An RE can begin with one of two specialdirector prefixes. If an RE begins with***:
, the rest of the RE is taken as an ARE. (This normally has no effect inPostgres Pro, since REs are assumed to be AREs; but it does have an effect if ERE or BRE mode had been specified by theflags
parameter to a regex function.) If an RE begins with***=
, the rest of the RE is taken to be a literal string, with all characters considered ordinary characters.
An ARE can begin withembedded options: a sequence(?
xyz
)
(wherexyz
is one or more alphabetic characters) specifies options affecting the rest of the RE. These options override any previously determined options — in particular, they can override the case-sensitivity behavior implied by a regex operator, or theflags
parameter to a regex function. The available option letters are shown inTable 9.20. Note that these same option letters are used in theflags
parameters of regex functions.
Table 9.20. ARE Embedded-option Letters
Option | Description |
---|---|
b | rest of RE is a BRE |
c | case-sensitive matching (overrides operator type) |
e | rest of RE is an ERE |
i | case-insensitive matching (seeSection 9.7.3.5) (overrides operator type) |
m | historical synonym forn |
n | newline-sensitive matching (seeSection 9.7.3.5) |
p | partial newline-sensitive matching (seeSection 9.7.3.5) |
q | rest of RE is a literal (“quoted”) string, all ordinary characters |
s | non-newline-sensitive matching (default) |
t | tight syntax (default; see below) |
w | inverse partial newline-sensitive (“weird”) matching (seeSection 9.7.3.5) |
x | expanded syntax (see below) |
Embedded options take effect at the)
terminating the sequence. They can appear only at the start of an ARE (after the***:
director if any).
In addition to the usual (tight) RE syntax, in which all characters are significant, there is anexpanded syntax, available by specifying the embeddedx
option. In the expanded syntax, white-space characters in the RE are ignored, as are all characters between a#
and the following newline (or the end of the RE). This permits paragraphing and commenting a complex RE. There are three exceptions to that basic rule:
a white-space character or
#
preceded by\
is retainedwhite space or
#
within a bracket expression is retainedwhite space and comments cannot appear within multi-character symbols, such as
(?:
For this purpose, white-space characters are blank, tab, newline, and any character that belongs to thespace
character class.
Finally, in an ARE, outside bracket expressions, the sequence(?#
ttt
)
(wherettt
is any text not containing a)
) is a comment, completely ignored. Again, this is not allowed between the characters of multi-character symbols, like(?:
. Such comments are more a historical artifact than a useful facility, and their use is deprecated; use the expanded syntax instead.
None of these metasyntax extensions is available if an initial***=
director has specified that the user's input be treated as a literal string rather than as an RE.
9.7.3.5. Regular Expression Matching Rules
In the event that an RE could match more than one substring of a given string, the RE matches the one starting earliest in the string. If the RE could match more than one substring starting at that point, either the longest possible match or the shortest possible match will be taken, depending on whether the RE isgreedy ornon-greedy.
Whether an RE is greedy or not is determined by the following rules:
Most atoms, and all constraints, have no greediness attribute (because they cannot match variable amounts of text anyway).
Adding parentheses around an RE does not change its greediness.
A quantified atom with a fixed-repetition quantifier (
{
m
}
or{
m
}?
) has the same greediness (possibly none) as the atom itself.A quantified atom with other normal quantifiers (including
{
m
,
n
}
withm
equal ton
) is greedy (prefers longest match).A quantified atom with a non-greedy quantifier (including
{
m
,
n
}?
withm
equal ton
) is non-greedy (prefers shortest match).A branch — that is, an RE that has no top-level
|
operator — has the same greediness as the first quantified atom in it that has a greediness attribute.An RE consisting of two or more branches connected by the
|
operator is always greedy.
The above rules associate greediness attributes not only with individual quantified atoms, but with branches and entire REs that contain quantified atoms. What that means is that the matching is done in such a way that the branch, or whole RE, matches the longest or shortest possible substringas a whole. Once the length of the entire match is determined, the part of it that matches any particular subexpression is determined on the basis of the greediness attribute of that subexpression, with subexpressions starting earlier in the RE taking priority over ones starting later.
An example of what this means:
SELECT SUBSTRING('XY1234Z', 'Y*([0-9]{1,3})');Result:123
SELECT SUBSTRING('XY1234Z', 'Y*?([0-9]{1,3})');Result:1
In the first case, the RE as a whole is greedy becauseY*
is greedy. It can match beginning at theY
, and it matches the longest possible string starting there, i.e.,Y123
. The output is the parenthesized part of that, or123
. In the second case, the RE as a whole is non-greedy becauseY*?
is non-greedy. It can match beginning at theY
, and it matches the shortest possible string starting there, i.e.,Y1
. The subexpression[0-9]{1,3}
is greedy but it cannot change the decision as to the overall match length; so it is forced to match just1
.
In short, when an RE contains both greedy and non-greedy subexpressions, the total match length is either as long as possible or as short as possible, according to the attribute assigned to the whole RE. The attributes assigned to the subexpressions only affect how much of that match they are allowed to“eat” relative to each other.
The quantifiers{1,1}
and{1,1}?
can be used to force greediness or non-greediness, respectively, on a subexpression or a whole RE. This is useful when you need the whole RE to have a greediness attribute different from what's deduced from its elements. As an example, suppose that we are trying to separate a string containing some digits into the digits and the parts before and after them. We might try to do that like this:
SELECT regexp_matches('abc01234xyz', '(.*)(\d+)(.*)');Result:{abc0123,4,xyz}
That didn't work: the first.*
is greedy so it“eats” as much as it can, leaving the\d+
to match at the last possible place, the last digit. We might try to fix that by making it non-greedy:
SELECT regexp_matches('abc01234xyz', '(.*?)(\d+)(.*)');Result:{abc,0,""}
That didn't work either, because now the RE as a whole is non-greedy and so it ends the overall match as soon as possible. We can get what we want by forcing the RE as a whole to be greedy:
SELECT regexp_matches('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');Result:{abc,01234,xyz}
Controlling the RE's overall greediness separately from its components' greediness allows great flexibility in handling variable-length patterns.
When deciding what is a longer or shorter match, match lengths are measured in characters, not collating elements. An empty string is considered longer than no match at all. For example:bb*
matches the three middle characters ofabbbc
;(week|wee)(night|knights)
matches all ten characters ofweeknights
; when(.*).*
is matched againstabc
the parenthesized subexpression matches all three characters; and when(a*)*
is matched againstbc
both the whole RE and the parenthesized subexpression match an empty string.
If case-independent matching is specified, the effect is much as if all case distinctions had vanished from the alphabet. When an alphabetic that exists in multiple cases appears as an ordinary character outside a bracket expression, it is effectively transformed into a bracket expression containing both cases, e.g.,x
becomes[xX]
. When it appears inside a bracket expression, all case counterparts of it are added to the bracket expression, e.g.,[x]
becomes[xX]
and[^x]
becomes[^xX]
.
If newline-sensitive matching is specified,.
and bracket expressions using^
will never match the newline character (so that matches will never cross newlines unless the RE explicitly arranges it) and^
and$
will match the empty string after and before a newline respectively, in addition to matching at beginning and end of string respectively. But the ARE escapes\A
and\Z
continue to match beginning or end of stringonly.
If partial newline-sensitive matching is specified, this affects.
and bracket expressions as with newline-sensitive matching, but not^
and$
.
If inverse partial newline-sensitive matching is specified, this affects^
and$
as with newline-sensitive matching, but not.
and bracket expressions. This isn't very useful but is provided for symmetry.
9.7.3.6. Limits and Compatibility
No particular limit is imposed on the length of REs in this implementation. However, programs intended to be highly portable should not employ REs longer than 256 bytes, as a POSIX-compliant implementation can refuse to accept such REs.
The only feature of AREs that is actually incompatible with POSIX EREs is that\
does not lose its special significance inside bracket expressions. All other ARE features use syntax which is illegal or has undefined or unspecified effects in POSIX EREs; the***
syntax of directors likewise is outside the POSIX syntax for both BREs and EREs.
Many of the ARE extensions are borrowed from Perl, but some have been changed to clean them up, and a few Perl extensions are not present. Incompatibilities of note include\b
,\B
, the lack of special treatment for a trailing newline, the addition of complemented bracket expressions to the things affected by newline-sensitive matching, the restrictions on parentheses and back references in lookahead constraints, and the longest/shortest-match (rather than first-match) matching semantics.
Two significant incompatibilities exist between AREs and the ERE syntax recognized by pre-7.4 releases ofPostgreSQL:
In AREs,
\
followed by an alphanumeric character is either an escape or an error, while in previous releases, it was just another way of writing the alphanumeric. This should not be much of a problem because there was no reason to write such a sequence in earlier releases.In AREs,
\
remains a special character within[]
, so a literal\
within a bracket expression must be written\\
.
9.7.3.7. Basic Regular Expressions
BREs differ from EREs in several respects. In BREs,|
,+
, and?
are ordinary characters and there is no equivalent for their functionality. The delimiters for bounds are\{
and\}
, with{
and}
by themselves ordinary characters. The parentheses for nested subexpressions are\(
and\)
, with(
and)
by themselves ordinary characters.^
is an ordinary character except at the beginning of the RE or the beginning of a parenthesized subexpression,$
is an ordinary character except at the end of the RE or the end of a parenthesized subexpression, and*
is an ordinary character if it appears at the beginning of the RE or the beginning of a parenthesized subexpression (after a possible leading^
). Finally, single-digit back references are available, and\<
and\>
are synonyms for[[:<:]]
and[[:>:]]
respectively; no other escapes are available in BREs.