I had been using [0-9]{9,12} all along to signify that the numeric string has a length of 9 or 12 characters. However I now realized that it will match input strings of length 10 or 11 as well. So I came out with the naive:
( [0-9]{9} | [0-9]{12} )
Is there a more succinct regex to represent this ?
You could save one character by using
but in my opinion your way is better because it conveys your intention more clearly. Regexes are hard enough to read already.
Of course you could use
\dinstead of[0-9].(Edit: I first thought you could drop the parens around
[0-9]{3}but you can’t; the question mark will be ignored. So you only save one character, not three.)(Edit 2: You will also need to anchor the regex with
^and$(or\b) orre.match()will also match123456789within1234567890.)