Postgres LIKE
does not support regular expressions.
You need the regular expression operator ~
.
Standard SQL also defines SIMILAR TO
as an odd mix of the above, but rather don't use that. See:
For finding if there is any character
... meaning any character at all:
... WHERE col <> ''; -- any character at all?
So neither NULL nor empty. See:
... meaning any alphabetic character (letter):
... WHERE col ~ '[[:alpha:]]'; -- any letters?
[[:alpha:]]
is the character class for all alphabetic characters - not just the ASCII letters [A-Za-z]
, includes letters like [ÄéÒçòý]
etc.
For finding if there is any number
... meaning any digit:
... WHERE col ~ '\d'; -- any digits?
\d
is the class shorthand for [[:digit:]]
.
For finding if there is any special character
... meaning anything except digits and letters:
... WHERE col ~ '\W'; -- anything but digits & letters?
\W
is the class shorthand for [^[:alnum:]_]
(underscore excluded - the manual is currently confusing there).
... meaning anything except digits, letters and plain space:
... WHERE col ~ '[^[:alnum:]_ ]' -- ... and space
That's the class shorthand \W
spelled out, additionally excluding plain space.
... meaning anything except digits, letters and any white space:
... WHERE col ~ '[^[:alnum:]_\s]' -- ... and any white space
... WHERE col ~ '[^[:alnum:]_[:space:]]' -- ... the same spelled out
This time excluding all white space as defined by the Posix character class space. About "white space" in Unicode:
... meaning any non-ASCII character:
If your DB cluster runs with UTF8 encoding, there is a simple, very fast hack:
... WHERE octet_length(col) > length(col); -- any non-ASCII letter?
octet_length()
counts the bytes in the string, while length()
(aliases: character_length()
or char_length()
) counts characters in the string. All basic ASCII characters ([\x00-\x7F]
) are encoded with 1 byte in UTF-8, all other characters use 2 - 4 bytes. Any non-ASCII character in the string makes the expression true
.
Further reading: