I am trying to get below constraint work in postgresql which checks the column wcode for the pattern. If the pattern doesn't match should throw an error.
CONSTRAINT wcoding CHECK (wcode::text ~ '[\w]{4,4}-[\w]{2,2}-[\w]{1,1}'::text);
Geniun input string is "AA14-AM-1". which actually works. but the problem is if I enter "AA14-AM-14" or "AA14-AM-1444" it doesn't through an error. I want to restrict input to use this ("AA14-AM-1") pattern.
{4,4}is redundant, it should be{4}. Also[\w]should be simply\w- for better reading your regex. + Also neither{1,1}, nor{1}is necessary.