i am trying to write a regular expression to validate the numbers with only one space in undefined places?
Maximum of 12 characters with one space or Maximum of 11 characters without spaces.
Ex: ‘25897 569874′,’5674′,’65783987665′,’435 6523’
i have tried with ^[0-9]{0,12}$.this is not perfect cause I don’t know how to place the spaces and its counts.
You can use this regex:
\d{1,11}will match from 1 to 11 digits without space.(?=\d+ \d+$)[\d ]{3,12}will match up to 11 digits with one space somewhere in the middle. The space cannot be leading or trailing, so' 23'will be rejected.(?=\d+ \d+$)is a look-ahead that matched one or more digit, then a space, then one or more digit, then anchor the end of string. It guarantees only one space will appear and the space will not be leading or trailing. The look-ahead also implicitly confirms that there are at least 3 characters in the string.[\d ]{3,12}will guarantee the string only contains digits or space, and up to 12 of them. The lower bound of number of repetition can be set to 3 or lower, since it has been implied by the look-ahead.The 2 constraints together guarantees that text contains from 1 to 11 digits and an optional space at arbitrary position in between the digits.
To allow leading space, but reject single space, empty string and trailing spaces:
Again, the look-ahead implies at least 2 characters, so the number of repetitions can be set to 2 or lower.