I need to write a regex expression to check if a serial number matches the following format:
XX-XXXXX-XXXXXX
Example: 50-99627-036043
A brief description of conditions:
- must be exactly 15 char long
- must only contain numbers – no letters
- dash separators at positions 2 and 8 (counting from 0)
I’ve generated the following with txt2re.com, but it doesn’t work and I’m not sure how to debug it:
(\\d)(\\d)(-)(\\d)(\\d)(\\d)(\\d)(\\d)(-)(\\d)(\\d)(\\d)(\\d)(\\d)(\\d)
Any help would be appreciated.
There is more than one dialect of regular expressions so, in the absence of knowledge as to which one you’re using, it’s probably best to only use constructs that they all usually have.
That would be:
Not all regex engines will support
\dand I’ve ensured you have start and end markers so that you get exactly the format you want (unless it matches the entire string, you may find it erroneously allowing stuff on either side of the 15 characters).If, for some bizarre reason, your regex engine doesn’t even support the
{m,n}-type counts, you’ll need to fully specify each digit: