Skip to content

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 FormLong FormDescriptionMatches
\d[[:digit:]]Digits0-9
\D[^[:digit:]]Non-digitsAll characters except 0-9
\w[[:alnum:]_]WordA-Z a-z 0-9 _
\W[^[:alnum:]_]Non-wordAll 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-spaceAll characters except [[:space:]]
\xHH[[:xdigit:]]Hexadecimal digitsA-F a-f 0-9
[[:ascii:]]ASCII characters0x00 - 0x7f
[[:extend:]]Extended characters0x0 - 0xff
[[:alpha:]]Alphabetic charactersA-Z a-z
[[:alnum:]]Alphanumeric charactersA-Z a-z 0-9
[[:lower:]]Lowercase lettersa-z
[[:upper:]]Uppercase lettersA-Z
[[:cntrl:]]Control characters0x00 - 0x1f and 0x7f
[[:graph:]]Visible characters0x21 - 0x7e
[[:print:]]Visible characters and spaces0x20 - 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:

EscapeDescription
\aalert (bell) character, as in C
\bbackspace, as in C
\Bsynonym for backslash (\) to help reduce the need for backslash doubling
\ethe character whose collating-sequence name is ESC
\fform feed, as in C
\nnewline, as in C
\rcarriage return, as in C
\thorizontal tab, as in C
\vvertical tab, as in C
\0the 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)
PatternMatches
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