I've tried the following regex, but it appears that nested "[]" are not allowed.
[\d[\s-]*]{9-23}
The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99. Something like ^[2-9][1-6]$ matches 21 or even 96! Any help would be appreciated.
Find Whitespace Using Regular Expressions in Java The most common regex character to find whitespaces are \s and \s+ . The difference between these regex characters is that \s represents a single whitespace character while \s+ represents multiple whitespaces in a string.
Occurrence Indicators (or Repetition Operators): +: one or more ( 1+ ), e.g., [0-9]+ matches one or more digits such as '123' , '000' . *: zero or more ( 0+ ), e.g., [0-9]* matches zero or more digits. It accepts all those in [0-9]+ plus the empty string.
Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.
You're on the right track, what you're looking for is probably:
(\d[\s-]*){8,22}\d
It appears that you don't want to match leading or trailing spaces and dashes, so the pattern that I think will work is:
^\d([- ]*\d){8,22}$
That is: one digit, followed by between 8 and 22 groups of zero or more dashes or spaces followed by a single digit.
Another solution which might be more obvious is this two-step solution:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With