简体   繁体   中英

Regex define two lists of allowed characters/digits

I'm working on a phone number regex and this is what I have so far: ^[0-9~+ ]{8,20}$

It allows digits between 0 and 9, and '+' and whitespace are allowed as well. The range has to be somewhere between 8 and 20. This works fine.

What I want further is to have 8 to 20 of digits only, where '+' and whitespace come separate from them.

Something like this: ^[+ ]\\d{8,20}$

Suggestions?

How about:

^(?:[+ ]*\d){8,20}$

This will match from 8 to 20 digits preceded or not by + or space.

If the + can be only at the beginig:

^[+]?(?:[ ]*\d){8,20}$

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