A colleague uses a application which uses regular expressions to validate data entry fields.
He needs to allows users to choose from digits 1-9.
i.e. 1, 2, 3, …, 12, 13, …, 23, 24, …, …, 123456789
The obvious basic [1-9]{1,9} would not disallow repeated digits or enforce numerical order.
A digit cannot be repeated (disallow 11, 343, etc.) and they must be in numerical order (disallow 21, 164, etc).
Short of matching the 320 possibilities separately “(1|2|3|…|12|13|…)”, how can I achieve this?
This one?
Clearly the user can insert up to 9 digits (123456789) and he can start from any one. Any digit is optional, but the order is fixed.
If you want a digit to be necessary, use a look ahead
or look behind
or negative look ahead
or negative look behind
so at least a digit is necessary
If your regex language doesn’t have look aheads (and look behinds) you can do:
Now the first digit “branches” to the “valid” combination of following optional digits.