简体   繁体   English

RegEx用于规范英国电话号码

[英]RegEx for normalising UK telephone number

I am trying to normalise UK telephone numbers to international format. 我正在尝试将英国电话号码标准化为国际格式。

The following strings should resolve to: +447834012345 以下字符串应解析为:+447834012345

  • 07834012345 07834012345
  • +447834012345 +447834012345
  • +4407834012345 +4407834012345
  • +44 (0) 7834 012345 +44(0)7834 012345
  • +44 0 7834 012345 +44 0 7834 012345
  • 004407834012345 004407834012345
  • 0044 (0) 7834012345 0044(0)7834012345
  • 00 44 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; 这还不能完全解决,因为我在以0开头等方面遇到问题; 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. 像我在这里的答案一样,我也建议您查看Google libphonenumber库。 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: 这是将其解析/标准化为PhoneNumber对象的方式:

 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: 此时,swissNumberProto包含:

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

PhoneNumber is a class that is auto-generated from the phonenumber.proto with necessary modifications for efficiency. PhoneNumber是从phonenumber.proto自动生成的类,并进行了必要的修改以提高效率。 For details on the meaning of each field, refer to https://github.com/googlei18n/libphonenumber/blob/master/resources/phonenumber.proto 有关每个字段含义的详细信息,请参阅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)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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