views:

197

answers:

2

8, 10, 12, 981 (few area codes in Sweden). Total phone number 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

+1  A: 

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
neo
~ 270 area codes in Sweden. your solution is per area code.http://en.wikipedia.org/wiki/Telephone_numbers_in_sweden
ms80
+1 for patience with poorly asked question.
Mladen Jablanović
+1  A: 

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})…$
Gumbo