简体   繁体   中英

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

I have following now.

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

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

Please 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM