-1
/^\(?([1-9]{1,3})\)??([0-9]{9})$/

这是为了非常有趣的数字。我是正则表达式的新手。你能解释一下它的作用吗?

4

3 回答 3

2

简单来说,我只是上传这张图片(来自http://www.regexper.com/#%2F%5E%5C(%3F(%5B1-9%5D%7B1%2C3%7D)%5C)% 3F%3F(%5B0-9%5D%7B9%7D)%24%2F )

在此处输入图像描述

于 2013-10-17T07:48:14.343 回答
2
/
 ^ <-- beginning of line
 \(? <-- optional "("
 ([1-9]{1,3}) <-- 1, 2, or 3 digits (each between 1 and 9) 
 \)?? <-- optional ")" (matches the first close parenthesis if multiple are present in the string)
 ([0-9]{9}) <-- 9 digits (each between 0 and 9)
 $ <-- end of line
/

它似乎与以地区/国家代码为前缀的电话号码相匹配

于 2013-10-17T07:54:25.447 回答
2

请参阅此自由间距模式。我假设你正在使用 PCRE

/^                #match the beginning of the string
 \(?              #match literal (, if exists
 (                #group 1
   [1-9]{1,3}     #match one, two, or three digit(s). The digit must be between 1-9
 )                #end of group1
 \)??             #match literal ), if exists
(                 #group 2
  [0-9]{9}        #match 9 digits, 0-9
)                 #end of group2
$/                #match the end of the string
于 2013-10-17T07:55:12.163 回答