Regular Expressions for UK Formats

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, 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.

Comments (5)

  1. Jason Row says:

    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.

  2. Ron says:


    The nice thing about RegExLib is you can submit regex patterns. Perhaps you could take an existing pattern and modify it appropriately for submission.

  3. Thanks Tim, this stuff is really usful.

  4. 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:


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

    (Specific towns)


    If starts with 01204 then format as xxxxx xxxxx [5,5]


    If starts with 015396 then format as xxxxxx xxxxx [6,5]


    If starts with 016977 then format as xxxxxx xxxx [6,4]

    Large Cities:

    If starts with 01 then format as xxxx xxx xxxx


    London and Corporates:

    If starts with 02 or 05 then format as xxx xxxx xxxx


    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:

    Warning I haven’t tested thoroughly and there may be mistakes. But am just sharing my thoughts if I may 🙂

  5. AriZona UK says:

    Few mistakes on the formula above (don’t use it!).

    But I can’t edit now.

    I will keep trying ..

Skip to main content