简体   繁体   English

正则表达式可能包含8或9个数字-怎么办?

[英]Regular Expression that might contain 8 or 9 numbers - how to do?

I use this regular expression to validate brazilian cellphones: 我使用此正则表达式来验证巴西手机:

/^(\([0-9]{2}\))\s([9]{1})?([0-9]{4})-([0-9]{4})$/

Output: (31) 8876-3234 VALID 输出: (31) 8876-3234有效

But Sao Paulo state has 9 digits cellphones: 但是圣保罗州有9位数字的手机:

Output SP : (31) 98876-3234 输出SP(31) 98876-3234

Current regex needs tweak to accept that as valid too, any tips on what should I change in it? 当前的正则表达式也需要进行调整,以使其也同样有效,关于该如何更改的任何提示?

You can use {m,n} quantifier to create a range: 您可以使用{m,n}量词来创建范围:

/^(\([0-9]{2}\))\s([0-9]{4,5})-([0-9]{4})$/

Though your regex seems to be fine, as it has an optional 9 to match before the 4 digits. 尽管您的正则表达式看起来不错,但它有一个可选的9可以匹配4位数字。 But if there is any possibility that there can be anything else than 9 there, then you can use this regex. 但是,如果存在9以外的任何其他可能性,则可以使用此正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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