Java 正则表达式 : For Phone number without leading space or 0 or +

标签 java regex

我想在电话号码中开始实际数字字符之前删除前导 0 、 + 或任何空格 (\s)。可能这并不难,但由于我是 regix 的新手,所以我正在寻求帮助。我尝试自己制作,但没有成功。

这是一个类似的链接,但它也添加了前导+,但我不想要这样。 trim phone number with regex

所以我尝试了这个,但它也删除了内部 0

(^\s*|0*|[+]*)

我也尝试过这个,但它实际上不能在java中工作,而只能在Php中工作,所以我需要基于java的regix的帮助

^(?:\s*0+|[+]0*|(\d+)0*)(?!$)

输入示例

+490232345678
0049032345678
+1 (555) 234-5078
+7 (23) 45/6789+10
(0123) 345/5678, ext. 666

期望的输出

490232345678
49032345678
15552345678
72345678910
1233455678666

我只需要 regix,因为我已经知道如何在 java 中使用该 regix。 我有这段代码需要regix

    String value = "+490232345678";
    Pattern p = Pattern.compile("(^\s*|0*|[+]*)");      
    Matcher m = p.matcher(value);
    value = m.replaceAll("");   

最佳答案

试试这个:

public static void main(String[] args) {
    String [] testResult = {"+490232345678", 
                            "0049032345678", 
                            "+1 (555) 234-5078", 
                            "+7 (23) 45/6789+10", 
                            "(0123) 345/5678, ext. 666"};
    String reg = "^([\\(+ ]0| +|0+|\\(\\)|\\+)| +|[^\\d]+|/$";
    for (String phone : testResult) {
        System.out.println(phone.replaceAll(reg, ""));
    }
}

输出将是:

490232345678
49032345678
15552345078
72345678910
1233455678666

更简单的方法是分两步完成:

 .replaceAll("[^\\d]+", "").replaceAll("^0+", "")

删除所有非数字,然后删除前导零。

正则表达式说明

^([\(+ ]0| +|0+|\(\)|\+)| +|[^\d]+|\/$

 1st Alternative ^([\(+ ]0| +|0+|\(\)|\+)
     ^ asserts position at start of the string
   1st Capturing Group ([\(+ ]0| +|0+|\(\)|\+)
       1st Alternative [\(+ ]0
           Match a single character present in the list below [\(+ ]
           \( matches the character ( literally (case sensitive)
           +  matches a single character in the list +  (case sensitive)
           0 matches the character 0 literally (case sensitive)
       2nd Alternative  +
           + matches the character   literally (case sensitive)
           + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
       3rd Alternative 0+
           0+ matches the character 0 literally (case sensitive)
           + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
       4th Alternative \(\)
           \( matches the character ( literally (case sensitive)
           \) matches the character ) literally (case sensitive)
       5th Alternative \+
           \+ matches the character + literally (case sensitive)
 2nd Alternative  +
    + matches the character   literally (case sensitive)
    + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
 3rd Alternative [^\d]+
    Match a single character not present in the list below [^\d]+
    + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
    \d matches a digit (equal to [0-9])
 4th Alternative \/$
    \/ matches the character / literally (case sensitive)
 $ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)

解释来源:https://regex101.com/

这是 https://www.debuggex.com/ 的视觉表示

enter image description here

关于Java 正则表达式 : For Phone number without leading space or 0 or +,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47472516/

相关文章:

java - Spring Boot 登录登录返回 401 并匹配凭据

正则表达式 : Grouping in a group

java - 使用 Java 中的正则表达式替换 yyyy-MM-dd 模式末尾的特定符号

php - 如何从字符串中删除(大多数)短词

java - 如何在java中获取解决方案X prolog

java - Spring Boot 忽略@JsonDeserialize 和@JsonSerialize

java - 根据数字条件限制列表/数组上的 iBATIS 迭代

javascript - 如何从其他Web项目调用rest api

c# RegEx 仅显示一次捕获

c# - 计算我的数组中的字符串在文本文件中找到的次数