【问题标题】:How to extract all numbers from string using find() method?如何使用 find() 方法从字符串中提取所有数字?
【发布时间】:2019-07-30 20:16:04
【问题描述】:

我有一个包含数字的字符串,但不仅是数字(实际上任何字母数字字符都可能存在),我必须从该字符串中提取每个有效的正整数并将其添加到列表中。

我尝试过使用 find() 方法和模式“\d+”,但是当输入字符串以整数开头时会出现问题。不会提取输入字符串开头的那个整数。

Pattern reg = Pattern.compile("\\d+");

Matcher test = reg.matcher( "988 5454 754");

boolean matches = test.matches();

List<String> numbers = new ArrayList<>();

while (test.find()) {
            numbers.add(test.group(0));
}

我希望第一次迭代中的第 0 组包含 988,第二次迭代中包含 5454,第三次迭代中包含 754,但是当字符串以整数开头时,它无法提取数字 988。

我只提取数字 5454 和 754。

我的正则表达式模式有什么错误吗?

编辑: 我已经完成了代码。你可以看到,matches() 方法的调用导致了这里的问题。

【问题讨论】:

  • 你能发一个minimal reproducible example吗?我看不出为什么您的代码不会打印所有 3 个匹配项。
  • 我刚刚运行了您的代码并打印了 number 数组,它包含所有 3 个数字:988 5454 754。您能否发布一个重现您的错误的示例?

标签: java regex


【解决方案1】:

复制粘贴您的代码并添加一个系统输出。它正在打印 3 个数字。可能在阅读数字时您可能错过了第一个数字。检查您如何遍历列表。

    Pattern reg = Pattern.compile("\\d+");

    Matcher test = reg.matcher( "988 5454 754");

    List<String> numbers = new ArrayList<>();

    while (test.find()) {
                numbers.add(test.group(0));
    }

    Iterator<String> it = numbers.iterator();
    while(it.hasNext()) {
        System.out.println(it.next());
    }

输出:988 5454 第754章

【讨论】:

    【解决方案2】:

    这不使用 find 但它确实提供了替代方案。从字符串中取出整数并将它们放在列表或数组中。需要 Java 8+

          String str = "ksks9292ksks02020ksks,x,slsk20209229,ss0202kslsks";
    

    保留为字符串并放入List

          List<String> numbStrList =
                Arrays.stream(str.split("\\D+")).filter(a -> !a.equals("")).collect(
                      Collectors.toList());
          System.out.println(numbStrList);
    

    保留为字符串并放入array

          String[] numbStrArray =
                Arrays.stream(str.split("\\D+")).filter(a -> !a.equals("")).toArray(
                      String[]::new);
          System.out.println(Arrays.toString(numbStrArray));
    

    转换为整数并放入List

          List<Integer> numbList =
                Arrays.stream(str.split("\\D+")).filter(a -> !a.equals("")).map(
                      Integer::valueOf).collect(Collectors.toList());
          System.out.println(numbList);
    

    转换为整数并放入array of int

          int[] numbArray =
                Arrays.stream(str.split("\\D+")).filter(a -> !a.equals("")).map(
                      Integer::valueOf).mapToInt(a -> a).toArray();
    
          System.out.println(Arrays.toString(numbArray));
    

    输出是

    [9292, 02020, 20209229, 0202]
    [9292, 02020, 20209229, 0202]
    [9292, 2020, 20209229, 202]
    [9292, 2020, 20209229, 202]
    

    注意转换情况,前导零被删除。

    Here are the salient parts of how it works.
    
          List<Integer> numbList =
                // create a stream of the array from split.  The split
                // splits on anything that does not constitute an integer
                Arrays.stream(str.split("\\D+"))
    
                // remove any empty strings (can result when the string
                // starts with a non-integer).            
                .filter(a -> !a.equals(""))
    
                //convert to the actual value
                .map(Integer::valueOf)
    
                 // collect them in a list.
                .collect(Collectors.toList());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多