Appearance
Character Classes and Escaped Special Characters
Character Classes
Character classes specify the characters that will successfully match a single character from a single input string.
The following character classes are supported:
Short Form | Long Form | Description | Matches |
---|---|---|---|
\d | [[:digit:]] | Digits | 0-9 |
\D | [^[:digit:]] | Non-digits | All characters except 0-9 |
\w | [[:alnum:]_] | Word | A-Z a-z 0-9 _ |
\W | [^[:alnum:]_] | Non-word | All characters except A-Z a-z 0-9 _ |
\s | [[:space:]] | All whitespace characters, including the line breaks | \t \r \n \v \f and the space character: ' ' |
\S | [^[:space:]] | Non-space | All characters except [[:space:]] |
\xHH | [[:xdigit:]] | Hexadecimal digits | A-F a-f 0-9 |
[[:ascii:]] | ASCII characters | 0x00 - 0x7f | |
[[:extend:]] | Extended characters | 0x0 - 0xff | |
[[:alpha:]] | Alphabetic characters | A-Z a-z | |
[[:alnum:]] | Alphanumeric characters | A-Z a-z 0-9 | |
[[:lower:]] | Lowercase letters | a-z | |
[[:upper:]] | Uppercase letters | A-Z | |
[[:cntrl:]] | Control characters | 0x00 - 0x1f and 0x7f | |
[[:graph:]] | Visible characters | 0x21 - 0x7e | |
[[:print:]] | Visible characters and spaces | 0x20 - 0x7e | |
[[:punct:]] | Punctuation (and symbols) | ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~ | |
[[:blank:]] | Space and tab | ' ' \t |
Note: Long-form expressions are intended to be used in a bracket expression. For example, [:digit:] without double brackets would match a single character from this set: ('d', 'i, 'g', 't', ':').
Character Classes Examples
The following example filters strings with two non-word characters:
premdb=> select name from team where name ~ '\W\w+\W';
name
----------------------
Queens Park Rangers
West Bromwich Albion
West Ham United
(3 rows)
The following example filters two spaces:
premdb=> select name from team where name ~ '[[:space:]]\w+[[:space:]]';
name
----------------------
Queens Park Rangers
West Bromwich Albion
West Ham United
(3 rows)
The following example demonstrates how omitting one set of brackets will match strings containing the characters :
, s
, p
, a
, c
, e
in addition to the word characters in between:
premdb=> select name from team where name ~ '[:space:]\w+[:space:]';
name
-------------------------
Arsenal
Barnsley
Blackburn Rovers
Blackpool
Bolton Wanderers
Charlton Athletic
Chelsea
Crystal Palace
Ipswich Town
Leeds United
Leicester City
Liverpool
Manchester City
Manchester United
Newcastle United
Oldham Athletic
Queens Park Rangers
Sheffield United
Sheffield Wednesday
Southampton
Sunderland
Swansea City
Tottenham Hotspur
Wigan Athletic
Wolverhampton Wanderers
(25 rows)
Escaped Special Characters
Escaped special non-alphanumeric characters are treated as ordinary characters.
The following escaped special characters are supported:
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 |
\e | the character whose collating-sequence name is ESC |
\f | form feed, as in C |
\n | newline, as in C |
\r | carriage return, as in C |
\t | horizontal tab, as in C |
\v | vertical tab, as in C |
\0 | the character whose value is 0 (the null byte) |
\xHH | (where hh is exactly two digits) the character whose hexadecimal value is 0xhh |
\xy | (where xy is exactly two octal digits and is not a back reference) the character whose octal value is 0xy |
\xyz | (where xyz is exactly three octal digits and is not a back reference) the character whose octal value is 0xyz |
Bracket Expressions
Bracket expressions define a list of characters from which a match can be made. You can specify character ranges with the -
operator. You can negate a set of characters by specifying the ^
operator as the first character inside a bracket expression.
For example:
[a-e]
[^a-e]
Equivalent classes and collations are not supported in bracket expressions. Constraint escapes are not supported in bracket expressions.
Bracket Expressions Examples
The following simple example uses a table called words
that contains four strings:
premdb=# select * from words;
c1
-------
thee
thou
thy
thine
(4 rows)
Pattern | Matches |
---|---|
c1~'[iy]' | thy, thine |
c1~'i|y' | thy, thine |
c1~'(i|y)' | thy, thine |
c1~'(?:i|y)' | thy, thine |
c1~'ee' | thee |
c1~'[a-e]' | thee, thine |
c1~'[^a-e]' | thee, thou, thy, thine |
Parent topic:Regular Expression Details