简体   繁体   中英

RegEx for normalising UK telephone number

I am trying to normalise UK telephone numbers to international format.

The following strings should resolve to: +447834012345

  • 07834012345
  • +447834012345
  • +4407834012345
  • +44 (0) 7834 012345
  • +44 0 7834 012345
  • 004407834012345
  • 0044 (0) 7834012345
  • 00 44 0 7834012345

So far, I have got this:

"+44" + mobile.replaceAll("[^0-9]0*(44)?0*", "")

This doesn't quite cut it, as I am having problems with leading 0's etc; see table below. I'd like to try and refrain from using the global flag if possible.

Mobile              | Normalised         | 
--------------------+--------------------+------
07834012345         | +4407834012345     | FAIL
+447834012345       | +447834012345      | PASS
+4407834012345      | +447834012345      | PASS
+44 (0) 7834 012345 | +44783412345       | FAIL
+44 0 7834 012345   | +44783412345       | FAIL
004407834012345     | +44004407834012345 | FAIL
0044 (0) 7834012345 | +4400447834012345  | FAIL
00 44 0 7834012345  | +44007834012345    | FAIL
+4407834004445      | +447834004445      | PASS

Thanks

If you still want the regex I was able to get it working like this:

"+44" + System.out.println(replaceAll("[^0-9]", "")
  .replaceAll("^0{0,2}(44){0,2}0{0,1}(\\d{10})", "$2"));

EDIT: Changed the code to reflect failed tests. Removed non-numeric characters before running the regex.

EDIT: Update code based on comments.

Like my answer here , I would also suggest looking at the Google libphonenumber library. I know it is not regex but it does exactly what you want.

An example of how to do it in Java (it is available in other languages) would be the following from the documentation :

Let's say you have a string representing a phone number from Switzerland. This is how you parse/normalize it into a PhoneNumber object:

 String swissNumberStr = "044 668 18 00"; PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); try { PhoneNumber swissNumberProto = phoneUtil.parse(swissNumberStr, "CH"); } catch (NumberParseException e) { System.err.println("NumberParseException was thrown: " + e.toString()); } 

At this point, swissNumberProto contains:

 { "country_code": 41, "national_number": 446681800 } 

PhoneNumber is a class that is auto-generated from the phonenumber.proto with necessary modifications for efficiency. For details on the meaning of each field, refer to https://github.com/googlei18n/libphonenumber/blob/master/resources/phonenumber.proto

Now let us validate whether the number is valid:

 boolean isValid = phoneUtil.isValidNumber(swissNumberProto); // returns true 

There are a few formats supported by the formatting method, as illustrated below:

 // Produces "+41 44 668 18 00" System.out.println(phoneUtil.format(swissNumberProto, PhoneNumberFormat.INTERNATIONAL)); // Produces "044 668 18 00" System.out.println(phoneUtil.format(swissNumberProto, PhoneNumberFormat.NATIONAL)); // Produces "+41446681800" System.out.println(phoneUtil.format(swissNumberProto, PhoneNumberFormat.E164)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM