I’m trying have regex with the following requirements:
- can contain any characters (symbols, spaces) except @ symbol
- but should contains at least 7 digits, but no more than 20 digits
Should not match:
1234
abxz1234
a1b2c3 d
12345678901234567890123456789
@123456789
1234@56789
Should match:
1234567890
123. 45678
$sb w77 123456789
((12345. 678
123 456 789
(333) 444-77.33 ext 4422
2
Answers
How about using
[^@]
and then a simple range on the character length{7,20}
, ieYou can use
^[^d@]*(?:d[^d@]*){7,20}$
.^
is the start-of-input assertion.$
is the end-of-input assertion.[^d@]*
matches any sequence of characters that does not include a digit nor@
d
matches one digit.(?: )
is a (non-capturing) group so we can add a quantifier to it{7,20}
is the quantifier that requires the preceding pattern to repeat at least 7 times and at most 20.