Pavlo Bazilnskyy
Pavlo Bazilnskyy

Reputation: 336

Adding check for count of characters to regex expression

I have a regular expression:

"^([a-zA-Z])[a-zA-Z_-]*[\\w_-]*[\\S]$|^([a-zA-Z])[0-9_-]*[\\S]$|^[a-zA-Z]*[\\S]$

(from Java).

And I want to add a check for count of characters entered and I am doing it by adding {5,15} before the ending $:

"^([a-zA-Z])[a-zA-Z_-]*[\\w_-]*[\\S]$|^([a-zA-Z])[0-9_-]*[\\S]$|^[a-zA-Z]*[\\S]{5,15}$"

but it does not work.

Could you please help me by explaining what is wrong?

Upvotes: 1

Views: 852

Answers (1)

Joey
Joey

Reputation: 354566

Well, your change just checks whether there are 5 to 15 non-space characters in that specific option.

I think the usual way would be to prepend

(?=^.{5,15}$)

and put the rest in another group after that.

Upvotes: 4

Related Questions