开发者

Java regex - How to read numbers only 5 to 7 digits

开发者 https://www.devze.com 2023-03-12 07:31 出处:网络
I have following now. regex=^[0-9]{6}$ this is working for starting 0 to 9 and for 6 and 7 digit number.

I have following now.

regex=^[0-9]{6}$

this is working for starting 0 to 9 and for 6 and 7 digit number.

Ple开发者_如何学Goase suggest how can i add for 5, 6 and 7 digit numbers.


use the range form of the { } operator

^[0-9]{5,7}$

Range inside of {N,M} is demarcated with a comma and translates into a string with length between N and M inclusive.

This is notably different from the [ ] operator which is to describe a character group and uses the - symbol to demarcate its ranges.


^[0-9]{5,7}$ should do the job for you if I understood your question correctly also your regex ^[0-9]{6}$ will match exactly 6 digits not 6 or 7 as you stated.

0

精彩评论

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