Java RegEx:对于没有前导空格或0或+的电话号码

时间:2017-11-24 11:37:42

标签: java regex

我想在从电话号码开始实际数字字符之前删除前导0,+或任何空格(\ s)。可能它并不难,但因为我是新人,所以我一直在寻求帮助。我试着自己做,但我没有成功。

这是链接哪种相同,但它也添加了前导+但我不希望这样。 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("");   

1 个答案:

答案 0 :(得分:2)

试试这个:

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+", "")

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

RegEx说明

^([\(+ ]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

相关问题