从电话号码中删除破折号

发布于 2024-08-30 04:53:55 字数 138 浏览 8 评论 0原文

使用java的正则表达式可以用来过滤掉破折号“-”并从代表电话号码的字符串中打开右圆括号...

这样(234) 887-9999应该给出2348879999 同样,234-887-9999 应该给出 2348879999。

谢谢,

What regular expression using java could be used to filter out dashes '-' and open close round brackets from a string representing phone numbers...

so that (234) 887-9999 should give 2348879999
and similarly 234-887-9999 should give 2348879999.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

咋地 2024-09-06 04:53:55
phoneNumber.replaceAll("[\\s\\-()]", "");

正则表达式定义了一个由任何空白字符(\s,由于我们传入字符串而被转义为 \\s)、破折号(转义,因为破折号在字符类的上下文中意味着特殊的东西)和括号。

请参阅 String.replaceAll(String, String)

编辑

gunslinger47

phoneNumber.replaceAll("\\D", "");

用空字符串替换任何非数字。

phoneNumber.replaceAll("[\\s\\-()]", "");

The regular expression defines a character class consisting of any whitespace character (\s, which is escaped as \\s because we're passing in a String), a dash (escaped because a dash means something special in the context of character classes), and parentheses.

See String.replaceAll(String, String).

EDIT

Per gunslinger47:

phoneNumber.replaceAll("\\D", "");

Replaces any non-digit with an empty string.

筑梦 2024-09-06 04:53:55
    public static String getMeMyNumber(String number, String countryCode)
    {    
         String out = number.replaceAll("[^0-9\\+]", "")        //remove all the non numbers (brackets dashes spaces etc.) except the + signs
                        .replaceAll("(^[1-9].+)", countryCode+"$1")         //if the number is starting with no zero and +, its a local number. prepend cc
                        .replaceAll("(.)(\\++)(.)", "$1$3")         //if there are left out +'s in the middle by mistake, remove them
                        .replaceAll("(^0{2}|^\\+)(.+)", "$2")       //make 00XXX... numbers and +XXXXX.. numbers into XXXX...
                        .replaceAll("^0([1-9])", countryCode+"$1");         //make 0XXXXXXX numbers into CCXXXXXXXX numbers
         return out;

    }
    public static String getMeMyNumber(String number, String countryCode)
    {    
         String out = number.replaceAll("[^0-9\\+]", "")        //remove all the non numbers (brackets dashes spaces etc.) except the + signs
                        .replaceAll("(^[1-9].+)", countryCode+"$1")         //if the number is starting with no zero and +, its a local number. prepend cc
                        .replaceAll("(.)(\\++)(.)", "$1$3")         //if there are left out +'s in the middle by mistake, remove them
                        .replaceAll("(^0{2}|^\\+)(.+)", "$2")       //make 00XXX... numbers and +XXXXX.. numbers into XXXX...
                        .replaceAll("^0([1-9])", countryCode+"$1");         //make 0XXXXXXX numbers into CCXXXXXXXX numbers
         return out;

    }
汹涌人海 2024-09-06 04:53:55

我的工作解决方案的 Kotlin 版本 - (扩展函数)

fun getMeMyNumber(number: String, countryCode: String): String? {
    return number.replace("[^0-9\\+]".toRegex(), "") //remove all the non numbers (brackets dashes spaces etc.) except the + signs
        .replace("(^[1-9].+)".toRegex(), "$countryCode$1") //if the number is starting with no zero and +, its a local number. prepend cc
        .replace("(.)(\\++)(.)".toRegex(), "$1$3") //if there are left out +'s in the middle by mistake, remove them
        .replace("(^0{2}|^\\+)(.+)".toRegex(), "$2") //make 00XXX... numbers and +XXXXX.. numbers into XXXX...
        .replace("^0([1-9])".toRegex(), "$countryCode$1") //make 0XXXXXXX numbers into CCXXXXXXXX numbers
}

Kotlin version of working solution for me - (Extension Function)

fun getMeMyNumber(number: String, countryCode: String): String? {
    return number.replace("[^0-9\\+]".toRegex(), "") //remove all the non numbers (brackets dashes spaces etc.) except the + signs
        .replace("(^[1-9].+)".toRegex(), "$countryCode$1") //if the number is starting with no zero and +, its a local number. prepend cc
        .replace("(.)(\\++)(.)".toRegex(), "$1$3") //if there are left out +'s in the middle by mistake, remove them
        .replace("(^0{2}|^\\+)(.+)".toRegex(), "$2") //make 00XXX... numbers and +XXXXX.. numbers into XXXX...
        .replace("^0([1-9])".toRegex(), "$countryCode$1") //make 0XXXXXXX numbers into CCXXXXXXXX numbers
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文