Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java正则表达式:用于不带前导空格或0或+;_Java_Regex - Fatal编程技术网

Java正则表达式:用于不带前导空格或0或+;

Java正则表达式:用于不带前导空格或0或+;,java,regex,Java,Regex,在从电话号码中开始实际数字字符之前,我想删除前导0、+或任何空格(\s)。也许这并不难,但因为我对雷吉克斯还不熟悉,所以我一直在寻求帮助。我试着做我自己的,但没有成功 这是一个相同的链接,但它也添加了leading+,但我不希望这样 因此,我尝试了这一点,但它也删除了内部0 (^\s*|0*|[+]*) 我也尝试过这个,但实际上它在java中不起作用,而只在Php中起作用,所以我需要基于java的regix的帮助 ^(?:\s*0+|[+]0*|(\d+)0*)(?!$) 示例输入 +490

在从电话号码中开始实际数字字符之前,我想删除前导0、+或任何空格(\s)。也许这并不难,但因为我对雷吉克斯还不熟悉,所以我一直在寻求帮助。我试着做我自己的,但没有成功

这是一个相同的链接,但它也添加了leading+,但我不希望这样

因此,我尝试了这一点,但它也删除了内部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)
解释来源:

这是一个来自

试试这个:

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)
解释来源:

这是一个来自


伙计,我不需要任何代码。我只是在搜索正则表达式。我知道如何编写java代码。但是我正在寻找一个可以与我的input@Mubasher看看我的答案。伙计,我不需要任何代码。我只是在搜索正则表达式。我知道如何编写java代码。但是我正在寻找一个可以与我的input@Mubasher看看我的答案,谢谢你,乔治。Jorge,在我的应用程序中不可能有两个步骤,在我们的应用程序中,我们有一个通用验证器,它接受regix输入并将捕获组替换为替换字符串,所以我将使用您的第一个regix。这个validtor是通用的,只需更改regix和replacement,就可以应用于任何类型的数据。jorge,如果你能解释一下它是如何工作的,那将非常有帮助。因为我正在学习regix和regix中的新蜜蜂,所以这将有助于我的理解。如果你有时间的话。好的,我会的,等一下!奇怪的是,我也在尝试regex101.com,但没能成功。非常感谢,谢谢你,乔治。Jorge,在我的应用程序中不可能有两个步骤,在我们的应用程序中,我们有一个通用验证器,它接受regix输入并将捕获组替换为替换字符串,所以我将使用您的第一个regix。这个validtor是通用的,只需更改regix和replacement,就可以应用于任何类型的数据。jorge,如果你能解释一下它是如何工作的,那将非常有帮助。因为我正在学习regix和regix中的新蜜蜂,所以这将有助于我的理解。如果你有时间的话。好的,我会的,等一下!奇怪的是,我也在尝试regex101.com,但没能成功。非常感谢你。