正则表达式允许数字之间有空格,或者在第一个数字之前没有任何内容

huangapple go评论75阅读模式
英文:

Regex to allow space between numbers or nothing before the first one

问题

我有以下方法 - verifyPhones,我在其中使用了两个正则表达式。
第一个用于识别字符串是否有效,如果无效,我需要查找哪些数字无效。

我的问题是,当我有两个连续的有效数字 - 20255501252025550125,系统返回的只有其中一个错误,而不是整个字符串。

我如何改进我的正则表达式以实现这一点?
提前谢谢。

有效数字的定义:

任何有9个数字的数字,可以用字符“-”分隔,也可以不分隔。
示例:

> 000-000-0000

> 0001110000

以下是我的代码:

 public static String verifyPhones(String phones) {
    Pattern patternValidAllPhones = Pattern.compile("^(((\\d{3}[-]?){2}\\d{4})[ ]+)+$");
    Pattern patternToFindWrongPhones = Pattern.compile("([ ]+((\\d{3}[-]?){2}\\d{4})[ ]+)");

    phones = phones.replaceAll("\\r", " ").replaceAll("\\n", " ").concat(" ");
    Matcher matcherValidAllPhones = patternValidAllPhones.matcher(phones);

    if(!matcherValidAllPhones.matches()) {
      Matcher matcherToFindWrongPhones = patternToFindWrongPhones.matcher(phones);
      return matcherToFindWrongPhones.replaceAll("").trim();
    }

    return "";
  }
@Test
  public void verifyPhonesTest_whenInvalidPhones_thenReturnInvalidPhones() {

    String invalidPhones1 = "202-555*0125 202-555-0125 202-555-0125 202-555-0125";
    String invalidPhones2 = "202-555-0125202-555-0125 202-555-0125 202-555-0125";
    String invalidPhones3 = "202555*0125 202-555-0125 202-555-0125 202-555-0125";
    String invalidPhones4 = "2025550125 20255501252025550125";

    String result1 = PhonesService.verifyPhones(invalidPhones1);
    String result2 = PhonesService.verifyPhones(invalidPhones2);
    String result3 = PhonesService.verifyPhones(invalidPhones3);
    String result4 = PhonesService.verifyPhones(invalidPhones4);

    assertFalse(result1.isEmpty());
    assertEquals("202-555*0125", result1);

    assertFalse(result2.isEmpty());
    assertEquals("202-555-0125202-555-0125", result2);

    assertFalse(result3.isEmpty());
    assertEquals("202555*0125", result3);

    assertFalse(result4.isEmpty());
    assertEquals("20255501252025550125", result4);
  }
英文:

I have the method that follows - verifyPhones, I am using two regexs on it.
The first one is to identify if the String is valid, if not I need to search which numbers are not valid.

My problem is when I have two valid numbers together - 20255501252025550125, the system is returning only one of them as wrong instead of the whole string.

How can I improve my regex to have achieve that?
Thanks in advance.

Definition of valid number:

Any number that have 9 numbers, separated or not by the char -
Example:

> 000-000-0000

> 0001110000

Here is my code:

 public static String verifyPhones(String phones) {
    Pattern patternValidAllPhones = Pattern.compile("^(((\\d{3}[-]?){2}\\d{4})[ ]+)+$");
    Pattern patternToFindWrongPhones = Pattern.compile("([ ]+((\\d{3}[-]?){2}\\d{4})[ ]+)");

    phones = phones.replaceAll("\\r", " ").replaceAll("\\n", " ").concat(" ");
    Matcher matcherValidAllPhones = patternValidAllPhones.matcher(phones);

    if(!matcherValidAllPhones.matches()) {
      Matcher matcherToFindWrongPhones = patternToFindWrongPhones.matcher(phones);
      return matcherToFindWrongPhones.replaceAll("").trim();
    }

    return "";
  }
@Test
  public void verifyPhonesTest_whenInvalidPhones_thenReturneInvalidPhones() {

    String invalidPhones1 = "202-555*0125 202-555-0125 202-555-0125 202-555-0125";
    String invalidPhones2 = "202-555-0125202-555-0125 202-555-0125 202-555-0125";
    String invalidPhones3 = "202555*0125 202-555-0125 202-555-0125 202-555-0125";
    String invalidPhones4 = "2025550125 20255501252025550125";

    String result1 = PhonesService.verifyPhones(invalidPhones1);
    String result2 = PhonesService.verifyPhones(invalidPhones2);
    String result3 = PhonesService.verifyPhones(invalidPhones3);
    String result4 = PhonesService.verifyPhones(invalidPhones4);

    assertFalse(result1.isEmpty());
    assertEquals("202-555*0125", result1);

    assertFalse(result2.isEmpty());
    assertEquals("202-555-0125202-555-0125", result2);

    assertFalse(result3.isEmpty());
    assertEquals("202555*0125", result3);

    assertFalse(result4.isEmpty());
    assertEquals("20255501252025550125", result4);
  }

答案1

得分: 0

这是我的建议:

如果有效的数字必须彼此之间用空格分隔,那么你可以首先通过空格将字符串分割成多个片段,其中每个片段都将是一个数字。然后在每个片段上单独应用验证模式。不符合模式的片段将是无效的数字。

以下是一个示例:

private static final Pattern phonePattern = Pattern.compile("(\\d{3}[-]?){2}\\d{4}");

public static List<String> verifyPhones(String phones) {
    String[] numbers = phones.split("\\s+");
    List<String> wrongPhones = new ArrayList<>();
        
    for (String number : numbers) {
        if (!phonePattern.matcher(number).matches()) {
            wrongPhones.add(number);
        }
    }
    return wrongPhones;        
}

注意:我已经更改了方法的签名。现在它返回一个包含错误数字的List。你并不总是期望只有一个无效的数字,对吧?

英文:

Here's what I suggest:

If valid numbers have to be separated by a space from each other, then you can first split the String by spaces into pieces, where each piece is going to be a number. And then apply validation pattern on each piece separately. Those pieces that do not match the pattern are going to be invalid numbers.

Here's an example:

private static final Pattern phonePattern = Pattern.compile(&quot;(\\d{3}[-]?){2}\\d{4}&quot;);

public static List&lt;String&gt; verifyPhones(String phones) {
    String[] numbers = phones.split(&quot;\\s+&quot;);
    List&lt;String&gt; wrongPhones = new ArrayList&lt;&gt;();
        
    for (String number : numbers) {
        if (!phonePattern.matcher(number).matches()) {
            wrongPhones.add(number);
        }
    }
    return wrongPhones;        
}

Note: I've changed the method's signature. Now it returns a List of wrong numbers. You do not expect to always have only one invalid number, do you?

huangapple
  • 本文由 发表于 2020年9月25日 03:26:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64053126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定