I'm struggling with some PostgreSQL validation constraints with regular expressions.
For a name, I'm trying to define the allowed characters:
But a name must not begin or end with :
For this constraint, I think I'm ok with this since I can't insert any quoted characters:
CONSTRAINT CK_LASTNAME_BEG CHECK (LEFT(LASTNAME, 1) NOT IN (' ', '-', ''''))
CONSTRAINT CK_LASTNAME_END CHECK (RIGHT(LASTNAME, 1) NOT IN (' ', '-', ''''))
As for the allowed characters, it doesn't work. For the time being (I've only defined the French diacritical letters), I've set this:
CONSTRAINT CK_LASTNAME CHECK (LASTNAME ~ '[a-zéèàùâêîôûäëïöüçœA-ZÉÈÀÙÂÊÎÔÛÄËÏÖÜÇŒ'''' -]')
I can enter any character, a number, a parenthesis, etc.
I'd say that the constraint could simply be
CHECK (lastname ~ $pattern$^[[:alpha:]][- '[:alpha:]]*$$pattern$)
^
matches the beginning of the string[[:alpha:]]
matches a single alphabetic character[- '[:alpha:]]
matches an alphabetic character, space, hyphen or single quote*
means arbitrarily many of the previous pattern$
matches the end of the stringThere is one caveat: the meaning of [:alpha:]
depends on your lc_ctype
setting. If you created your database or defined your column with the C
or POSIX
locale, that will only match ASCII characters. For the best support of all possible characters, use a natural language ICU collation like "en-US-x-ico"
.