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

Working but ugly UK phone number regex.

Last post 08-17-2012, 4:10 AM by g1smd. 3 replies.
Sort Posts: Previous Next
  •  02-02-2011, 12:36 AM 77507

    Working but ugly UK phone number regex.

    Hi there,

    I want to validate UK phone numbers which are always 11 chars starting with a zero. That part is simple however, I would like to permit the user to also enter whitespace and/or hyphens. Opinion differs on where those characters should go so I'm happy to allow the user to do whatever they feel like and simply strip those chars out later. All the following should pass...

    07816924120

    078 1692 4120, 078-1692-4120

    0781 692 4120, 0781-692-4120

    07816 924 120, 07816-924-120

    07816 924120, 07816-924120 even...

    0 7 8 1 6 9 2 4 1 2 0 or...

    078-1692 4120

    Essentially as long as there's 11 digits (no more, no less), no consecutive whitespaces or hyphens, the first digit is a zero and the second digit is a non-zero digit it should match, everything else should fail. Happily I can ignore country codes and suchlike.

    I'm would like the same regex to work in both python and javascript.

    The regex I have does work but it's very ugly/ repetitive...

     ^[ -]{0,1}0[ -]{0,1}[1-9][ -]{0,1}[0-9][ -]{0,1}[0-9][ -]{0,1}[0-9][ -]{0,1}[0-9][ -]{0,1}[0-9][ -]{0,1}[0-9][ -]{0,1}[0-9][ -]{0,1}[0-9][ -]{0,1}[0-9]$

    Told you!

    I'm assuming there's got to be a neater / more concise way of writing this, is that a fair assumption? and if so what's the trick?

    Cheers,

    Roger Heathcote

  •  02-02-2011, 2:13 AM 77508 in reply to 77507

    Re: Working but ugly UK phone number regex.

    Matching on your given specs

    Raw Match Pattern:
    0[ -]?[1-9](?:[ -]?\d){9}\b

    Javascript Code Example:

    <script type="text/javascript">
      var re = /0[ -]?[1-9](?:[ -]?\d){9}\b/;
      var sourcestring = "source string to match with pattern";
      var results = [];
      var i = 0;
      for (var matches = re.exec(sourcestring); matches != null; matches = re.exec(sourcestring)) {
        results[i] = matches;
        for (var j=0; j<matches.length; j++) {
          alert("results["+i+"]["+j+"] = " + results[i][j]);
        }
        i++;
      }
    </script>


    $matches Array:
    (
        [0] => Array
            (
                [0] => 07816924120
                [1] => 078 1692 4120
                [2] => 078-1692-4120
                [3] => 0781 692 4120
                [4] => 0781-692-4120
                [5] => 07816 924 120
                [6] => 07816-924-120
                [7] => 07816 924120
                [8] => 07816-924120
                [9] => 0 7 8 1 6 9 2 4 1 2 0
                [10] => 078-1692 4120
            )

    )


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  02-02-2011, 11:33 PM 77543 in reply to 77508

    Re: Working but ugly UK phone number regex.

    That's great mash, thank you very much. I've also now understand grouping, repetition and backreferences a lot better having been initially puzzled by your reply and having subsequently googled around for an explaination of what the "?:" part does :)

    Cheers,

    Roger.

  •  08-17-2012, 4:10 AM 86191 in reply to 77543

    Re: Working but ugly UK phone number regex.

    The problem with your specification is that UK phone numbers are not all 10 digits after the 0 trunk prefix or +44 country code.

    Some 41 area codes also contain at least some 9 digit numbers. Additionally, all 0500 and some 0800 numbers have only 9 digits after the leading 0 or +44 prefix.

    If you're parsing input, don't allow only a leading 0. You should also allow +44, +44 (0), 00 44, 011 44, and so on.

    Let the user enter the number any way they like. Parse it to extract the NSN part, then validate that part has the right number of digits and is in a valid range.

    There's detailed listings at:

    http://www.aa-asterisk.org.uk/index.php/Number_format

    http://www.aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers

     

    The simplest form for validating "'00 or 011 or + followed by 44 and optional 0' OR a '0'  - followed by '9 digits and other stuff intermixed'" limiting "other stuff" to things like spaces, brackets, periods, hypens, etc, is:

        ^\(?(?:(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0)(([\s\(\).-]+\d){9,10})$

    The NSN part is in $1.

     

View as RSS news feed in XML