I would like to have a check constraint that enforces this regex pattern for a multi-part code:
^\d{3}\.\d{3}\.\d{4}$
…three numbers, a period, three numbers, a period, four numbers.
Do I need to create a CLR function to enforce the pattern matching, and can the CLR function be referenced in DDL?
Is there a way to enforce the pattern in another way, using LIKE
?
Best Answer
The regex in your question is not entirely unambiguous
So in many flavours it would match
١١١.١١١.١١١١
(that character being Arabic-Indic Digit One)I'm assuming that you just want to match latin
0-9
. You can useI'm using the binary collate clause so strings like
¾¾¾.¾¾¾.¾¾¾¾
or10².10².1000
don't pass the check (as can happen on some collations).Neither of these would match the desired CLR regex and although using the set syntax (
[0123456789]
) rather than the range syntax ([0-9]
) resolves the first one it is still collation dependent whether2
matches²
or not.I'm using
REPLACE
rather than hard-coding theLIKE
pattern because I find it more readable what the pattern is checking. It is easier to see three digits and a dot followed by three digits another dot then four digits in that format than parsing[0-9][0-9][0-9].[0-9] [0-9][0-9].[0-9][0-9][0-9][0-9]
.It also makes it easier to swap out implementation.
I you do decide that you want the full blown semantics of the CLR regular expression after all you could just change
'[0-9]'
above to the below (and likely change the column toNCHAR(12)
in that case)