Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Help for Belgian phone numbers validation with regexp

Last post 08-17-2012, 3:09 PM by g1smd. 3 replies.
Sort Posts: Previous Next
  •  02-09-2009, 8:02 AM 50792

    Help for Belgian phone numbers validation with regexp

    Hello,

    I'm really not a regexp expert. I tried studying on it, but I just don't succeed in producing my own working regexp... and I really need one to check for validity on Belgian phone numbers (both regular phone and cellphone numbers).

    I have started from a regexp for us phone numbers on the site: http://regexlib.com/Search.aspx?k=phone, but the way Belgian phone numbers are entered on a web form is different from an US phone number.

    In Belgian, regular phone numbers are 9 digits long, and they are entered as:

    055 60 60 60, 02 555 56 56, 055/60 60 60, 055606060, 055/60.60.60, 055.60.60.60, 055 606 606, 055/606 606, 09/366 36 36

    Cellphone numbers always start with 04, and are always 10 digits long:

    0474 74 74 74, 0474 744 744, 0474/744 744, 0474.74.74.74, etc.

    I really hope somebody could please help me with a working regexp... I'm already trying for some hours producing my own regexp, but unfortunately I just don't succeed... :(

    Thanks a lot!!

    Kind regards,

    Geoffrey Beulque

  •  02-09-2009, 12:54 PM 50803 in reply to 50792

    Re: Help for Belgian phone numbers validation with regexp

    Raw Match Pattern:
    (?:\d{3}[/. ]?(?:(?:\d\d([. ]?)\d\d\1\d\d)|(?:\d{3}\x20\d{3})))|(?:\d\d[ /]\d{3}(?:\x20\d\d){2})|(?:04\d\d[/ ](?:\d{3}\x20\d{3}|(?:\d\d[. ]){2}\d\d))

    Match Pattern Explanation:
    The regular expression:

    (?-imsx:(?:\d{3}[/. ]?(?:(?:\d\d([. ]?)\d\d\1\d\d)|(?:\d{3}\x20\d{3})))|(?:\d\d[ /]\d{3}(?:\x20\d\d){2})|(?:04\d\d[/ ](?:\d{3}\x20\d{3}|(?:\d\d[. ]){2}\d\d)))

    matches as follows:

    NODE EXPLANATION
    ----------------------------------------------------------------------
    (?-imsx: group, but do not capture (case-sensitive)
    (with ^ and $ matching normally) (with . not
    matching \n) (matching whitespace and #
    normally):
    ----------------------------------------------------------------------
    (?: group, but do not capture:
    ----------------------------------------------------------------------
    \d{3} digits (0-9) (3 times)
    ----------------------------------------------------------------------
    [/. ]? any character of: '/', '.', ' '
    (optional (matching the most amount
    possible))
    ----------------------------------------------------------------------
    (?: group, but do not capture:
    ----------------------------------------------------------------------
    (?: group, but do not capture:
    ----------------------------------------------------------------------
    \d digits (0-9)
    ----------------------------------------------------------------------
    \d digits (0-9)
    ----------------------------------------------------------------------
    ( group and capture to \1:
    ----------------------------------------------------------------------
    [. ]? any character of: '.', ' '
    (optional (matching the most
    amount possible))
    ----------------------------------------------------------------------
    ) end of \1
    ----------------------------------------------------------------------
    \d digits (0-9)
    ----------------------------------------------------------------------
    \d digits (0-9)
    ----------------------------------------------------------------------
    \1 what was matched by capture \1
    ----------------------------------------------------------------------
    \d digits (0-9)
    ----------------------------------------------------------------------
    \d digits (0-9)
    ----------------------------------------------------------------------
    ) end of grouping
    ----------------------------------------------------------------------
    | OR
    ----------------------------------------------------------------------
    (?: group, but do not capture:
    ----------------------------------------------------------------------
    \d{3} digits (0-9) (3 times)
    ----------------------------------------------------------------------
    \x20 character 32
    ----------------------------------------------------------------------
    \d{3} digits (0-9) (3 times)
    ----------------------------------------------------------------------
    ) end of grouping
    ----------------------------------------------------------------------
    ) end of grouping
    ----------------------------------------------------------------------
    ) end of grouping
    ----------------------------------------------------------------------
    | OR
    ----------------------------------------------------------------------
    (?: group, but do not capture:
    ----------------------------------------------------------------------
    \d digits (0-9)
    ----------------------------------------------------------------------
    \d digits (0-9)
    ----------------------------------------------------------------------
    [ /] any character of: ' ', '/'
    ----------------------------------------------------------------------
    \d{3} digits (0-9) (3 times)
    ----------------------------------------------------------------------
    (?: group, but do not capture (2 times):
    ----------------------------------------------------------------------
    \x20 character 32
    ----------------------------------------------------------------------
    \d digits (0-9)
    ----------------------------------------------------------------------
    \d digits (0-9)
    ----------------------------------------------------------------------
    ){2} end of grouping
    ----------------------------------------------------------------------
    ) end of grouping
    ----------------------------------------------------------------------
    | OR
    ----------------------------------------------------------------------
    (?: group, but do not capture:
    ----------------------------------------------------------------------
    04 '04'
    ----------------------------------------------------------------------
    \d digits (0-9)
    ----------------------------------------------------------------------
    \d digits (0-9)
    ----------------------------------------------------------------------
    [/ ] any character of: '/', ' '
    ----------------------------------------------------------------------
    (?: group, but do not capture:
    ----------------------------------------------------------------------
    \d{3} digits (0-9) (3 times)
    ----------------------------------------------------------------------
    \x20 character 32
    ----------------------------------------------------------------------
    \d{3} digits (0-9) (3 times)
    ----------------------------------------------------------------------
    | OR
    ----------------------------------------------------------------------
    (?: group, but do not capture (2 times):
    ----------------------------------------------------------------------
    \d digits (0-9)
    ----------------------------------------------------------------------
    \d digits (0-9)
    ----------------------------------------------------------------------
    [. ] any character of: '.', ' '
    ----------------------------------------------------------------------
    ){2} end of grouping
    ----------------------------------------------------------------------
    \d digits (0-9)
    ----------------------------------------------------------------------
    \d digits (0-9)
    ----------------------------------------------------------------------
    ) end of grouping
    ----------------------------------------------------------------------
    ) end of grouping
    ----------------------------------------------------------------------
    ) end of grouping
    ----------------------------------------------------------------------

    $matches Array:
    (
    [0] => Array
    (
    [0] => 055 60 60 60
    [1] => 02 555 56 56
    [2] => 055/60 60 60
    [3] => 055606060
    [4] => 055/60.60.60
    [5] => 055.60.60.60
    [6] => 055 606 606
    [7] => 055/606 606
    [8] => 09/366 36 36
    [9] => 0474 74 74 74
    [10] => 0474 744 744
    [11] => 0474/744 744
    [12] => 474.74.74.74
    )

    [1] => Array
    (
    [0] =>
    [1] =>
    [2] =>
    [3] =>
    [4] => .
    [5] => .
    [6] =>
    [7] =>
    [8] =>
    [9] =>
    [10] =>
    [11] =>
    [12] => .
    )

    )

    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  02-11-2009, 9:11 AM 50848 in reply to 50803

    Re: Help for Belgian phone numbers validation with regexp

    Hi,

    Thank you very very much for this great reply with explanation!!

    I finally succeeded in resolving my problem!!

    Thanks :)

    Kind regards,

    Geoffrey

  •  08-17-2012, 3:09 PM 86198 in reply to 50848

    Re: Help for Belgian phone numbers validation with regexp

    I prefer to break the job into several parts.

    The first matches the +32, 00 32, 011 32 or 0 prefix, folowed by a number of digits. This pattern extracts the NSN part of the number:

     ^\(?(?:(?:0(?:0|11)[\s\(\).-]+|\+)32[0\s\(\).-]+|0)(([\s\(\).-]+\d){8,9})$  <-- assumes NSN is 8 or 9 digits.

    The second pattern checks the valid ranges and number lengths using just the NSN.

    ^([478]\d{7}|[2345]\d{8})$<-- this isn't the real pattern for Belgium.

    Those patterns allow the user to enter the number in almost any format they want with spaces hyphens and brackets in almost any combination.

    The above patterns check what a user entered and turn it into a standardised format without any spaces.

    If the number needs to be displayed, this is a simple matter of breaking the stored number into constituent parts by number range checked by initial digits.

View as RSS news feed in XML