Regular Expressions for UK Formats

Regular Expressions for UK Formats

Rate This
  • Comments 6

It's often hard to find good UK-aware regular expressions (for use with the ASP.NET RegularExpressionValidator or the .NET RegEx class). I was therefore delighted to come across RegExLib.com, which contains a most comprehensive listing of commonly used regular expressions. I wish more US sites would take this into account too - not everyone has a telephone number of the form (xxx) xxx-xxxx!

Some of the most useful ones:

One big gap is for UK telephone numbers - as with other European countries such as Germany, the number format can vary depending on the area code. There are several expressions listed at RegExLib, but none of them properly match the Ofcom recommended presentation style. Has anyone found a better regular expression that they'd like to share?

Also interesting to see a regular expression blog - worth tracking if you deal with these issues on a regular basis. Thanks to the mswebdev mailing list (run by Phil and Chris) for this pointer.

  • Thanks for the link Tim. Just a couple of days ago I put together my own regular expression pattern for entering times and was wondering if there was anything I missed. The RegEx Library site had a few variations and showed that I wasn't that far off.
  • Tim,

    The nice thing about RegExLib is you can submit regex patterns. Perhaps you could take an existing pattern and modify it appropriately for submission.
  • Thanks Tim, this stuff is really usful.
  • Dropped by to see if there was a good regex here.

    I am building a validating form,
    it won't validate numbers but will reformat them.

    The only validation that will take place is its length (if shorter than 10 digits) and funny symbols other than -,+,(,)

    Building a regex will be hard (although ideal).
    So I am trying to just go about removing all non-digits and spaces (ie ending up with 02087805441) and then reformating via the following rules:

    Mobiles:
    If starts with 07 then format as xxxxx xxxxxx ie [5,6]

    (Specific towns)

    Bolton:
    If starts with 01204 then format as xxxxx xxxxx [5,5]
    Sedbergh:
    If starts with 015396 then format as xxxxxx xxxxx [6,5]
    Brampton:
    If starts with 016977 then format as xxxxxx xxxx [6,4]

    Large Cities:
    If starts with 01 then format as xxxx xxx xxxx
    [4,3,4]
    London and Corporates:
    If starts with 02 or 05 then format as xxx xxxx xxxx
    [3,4,4]

    The formula above wouldn't consider many other types such as freephone numbers and other services.
    All rules are mutually exclusive.
    If nothing matches then leave number unformatted.

    The formulas I've grabbed from here:
    http://en.wikipedia.org/wiki/UK_telephone_numbering_plan

    Warning I haven't tested thoroughly and there may be mistakes. But am just sharing my thoughts if I may :)

  • Few mistakes on the formula above (don't use it!).
    But I can't edit now.
    I will keep trying ..
Page 1 of 1 (6 items)