java 从电话号码中删除括号、破折号和空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25089362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:19:08  来源:igfitidea点击:

Remove parentheses, dashes, and spaces from phone number

javaregex

提问by user3786942

I have a phone number like (123) 456-7890. I am using the replaceAllmethod to remove ()and -and spaces from the string. I tried following

我有一个电话号码,如 (123) 456-7890。我正在使用该replaceAll方法从字符串中删除()-和空格。我试过跟随

String phNo= "(123) 456-7890".replaceAll("[()-\s]").trim();

but it's not working. Any solution?

但它不起作用。有什么解决办法吗?

回答by anubhava

This should work:

这应该有效:

String phNo = "(123) 456-7890".replaceAll("[()\s-]+", "");

In your regex:

在您的正则表达式中:

  • \sshould be \\s
  • Hyphen should be first or last in character class to avoid escaping or use it as \\-
  • Use quantifier +as in [()\\s-]+to increase efficiency by minimizing # of replacements
  • \s应该 \\s
  • 连字符应该在字符类中的第一个或最后一个以避免转义或将其用作 \\-
  • 使用量词+as in[()\\s-]+通过最小化替换次数来提高效率

回答by Andie2302

If you want the phone number then use:

如果您想要电话号码,请使用:

String phNo = "(123) 456-7890".replaceAll("\D+", "");

This regex will mark all characters that are not digits, and replace them with an empty string.

此正则表达式将标记所有不是数字的字符,并用空字符串替换它们。



The regex: \D+

正则表达式: \D+

  • Match a single character that is not a digit. \D
    • Between one and unlimited times, as many times as possible. +
  • 匹配非数字的单个字符。 \D
    • 在一次和无限次之间,尽可能多次。 +

回答by katwal-Dipak

  String newStr = phoneNumber.replaceAll("[^0-9]", "");
        System.out.println(newStr);

Removes All Non-Digit Characters.

删除所有非数字字符。

Java Regex - Tutorial

Java 正则表达式 - 教程

回答by rgettman

The -character with brackets []indicates a character range, e.g. [a-z]. However, the character range doesn't work here where you want a literal -to be used. Escape it.

-用括号字符[]指示字符范围内,例如[a-z]。但是,字符范围在您想要使用文字的地方不起作用-。逃离它。

String phNo = "(123) 456-7890".replaceAll("[()\-\s]", "").trim());

回答by hwnd

There are two main reasons this does not work as expected.

这没有按预期工作有两个主要原因。

  1. Inside of a character class the hyphen has special meaning. You can place a hyphen as the first or last character of the class. In some regular expression implementations, you can also place directly after a range. If you place the hyphen anywhere else you need to escape it in order to add it to your class.

    String phNo = "(123) 456-7890".replaceAll("[()\-\s]").trim();
                                                  ^^
    
  2. You are not supplying a replacement value which neither answer has pointed out to you.

    String phNo = "(123) 456-7890".replaceAll("[()\-\s]", "").trim();
                                                            ^^
    

    And finally, you can remove .trim()here as well.

    String phNo = "(123) 456-7890".replaceAll("[()\-\s]", "");
    
  1. 在字符类中,连字符具有特殊含义。您可以将连字符作为类的第一个或最后一个字符。在一些正则表达式的实现中,你也可以直接放在一个范围之后。如果您将连字符放在其他任何地方,则需要将其转义以将其添加到您的类中。

    String phNo = "(123) 456-7890".replaceAll("[()\-\s]").trim();
                                                  ^^
    
  2. 您没有提供两个答案都没有向您指出的替代价值。

    String phNo = "(123) 456-7890".replaceAll("[()\-\s]", "").trim();
                                                            ^^
    

    最后,您也可以.trim()在此处删除。

    String phNo = "(123) 456-7890".replaceAll("[()\-\s]", "");
    

回答by Mohit Suthar

If you are using Kotlin than

如果您使用的是 Kotlin,那么

mobileNo.replace(Regex("[()\\-\\s]"), "")

mobileNo.replace(Regex("[()\\-\\s]"), "")