开发者

Adding check for count of characters to regex expression

开发者 https://www.devze.com 2023-03-09 14:16 出处:网络
I have a regular expression: \"^([a-zA-Z])[a-zA-Z_-]*[\\\\w_-]*[\\\\S]$开发者_如何学C|^([a-zA-Z])[0-9_-]*[\\\\S]$|^[a-zA-Z]*[\\\\S]$

I have a regular expression:

"^([a-zA-Z])[a-zA-Z_-]*[\\w_-]*[\\S]$开发者_如何学C|^([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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消