开发者

regex && logical condition (telephone numbers in Sweden)

8, 10, 12, 981 (few area codes in Sweden). Total phone number开发者_JAVA百科 can be 10 or 11 (digits only) If 8 + 9 or 10 digits if 981 + 7 or 8 digits Can this be done in regex?

something like that ..hm (8|10|12)\d{n} => Total Length 10 or 11


You will probably need to treat the different cases (i.e. area code length) separately, like:

^(8\d{9,10}|(10|12)\d{8,9}|981\d{7,8})$

Or you use a look-ahead or look-behind assertions:

^(?=\d{10,11})…$


What about ^(?:8\d{9,10}|(?:10|12)\d{8,9}$?

Edit: Then don't do it in regex. Pseudocode:

function check(number):
  array areaCodes = array(8, 10, 12, 981)
  if !number ~= '^\d{10,11}$':
    return false
  foreach in areaCodes as code:
    if (substring(number, 0, length(code) - 1) == code) return true
  return false


An attempt I made for The Netherlands which has both 2 and 3 digit area codes with respectively 7 and 6 digit subscriber numbers (and some unfinished other length routes):

\+31(?:(?:(?#AREA2D)1[035]|2[0346]|3[03568]|4[0356]|5[0358]|7\d)[2-8]\d{6}|(?:(?#AREA3D)11[134578]|16[124-8]|17[24]|18[0-467]|22[2346-9]|25[125]|29[479]|31[3-8]|32[01]|34[1-8]|41[12368]|47[58]|48[15-8]|49[23579]|51[1-9]|52[1-5789]|54[13-8]|56[126]|57[0-3578]|59[1-9])[2-8]\d{5}|(?:(?#OTHER10D)6[1-68]|8[4578]|91)\d{7})
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜