【问题标题】:Trying to find second integer in string - Not working试图在字符串中找到第二个整数 - 不工作
【发布时间】:2021-03-27 18:49:35
【问题描述】:

我一直在尝试使用正则表达式在字符串中查找某些整数(我不完全确定它是什么,有人可以向我推荐一些东西来了解它吗?),但是有一些问题。我已经开始寻找字符串中的第一个整数并将该整数值设置为一个变量,但是在寻找第二个整数时被卡住了。这是我目前的代码:

for (int x = 0; x < list.length; x++) {
                    
    current = list[x];
                                                
    Matcher first = Pattern.compile("\\d+").matcher(current);
    first.find();
    min = Integer.valueOf(first.group());
                    
    Matcher second = Pattern.compile("^\\D*\\d+\\D+(\\d+)").matcher(current);
    second.find();
    max = Integer.valueOf(second.group());
                    
    System.out.println("min: " + min + " max: " + max);
                    
                    
}

在这种情况下,假设字符串是“1 guacamole 36”,我希望它将min 变量设置为1,将max 设置为36。我该怎么做?所有出现的都是错误,我假设来自第二个匹配器的部分。

请让我知道您是否可以帮助查询或者我是否应该添加更多信息 - 提前致谢!

附: list 是一个字符串数组,我试图在其中为每个字符串,每次将最小值和最大值重置为字符串中的整数。

【问题讨论】:

标签: java regex for-loop matcher


【解决方案1】:

调用group()会找到组0(即整场比赛),但是你想要的号码被捕获在组1中,所以你应该这样做:

max = Integer.valueOf(second.group(1));

您实际上不需要第二个正则表达式来查找第二个数字。只需要求第一个匹配器再次匹配。它会找到第二个数字:

Matcher first = Pattern.compile("\\d+").matcher(current);
first.find();
min = Integer.valueOf(first.group());

first.find(); // call find again!
max = Integer.valueOf(first.group());

一个更安全的方法是在调用group之前检查匹配器是否真的找到了任何东西:

Matcher first = Pattern.compile("\\d+").matcher(current);
if (first.find()) {
    min = Integer.valueOf(first.group());


    if (first.find()) { // call find again!
        max = Integer.valueOf(first.group());
    } else {
        // can't find a second number!
    }
} else {
    // can't find any numbers!
}

【讨论】:

  • 这非常好用!太感谢了!另外,是否有可能获得找到的第二个数字的位置,例如使用索引?在我正在测试的字符串中,每个都设置为这种格式:“1-36 g: guacamole”,所以我想知道是否有办法找到两个整数后面的字母,以及后面的完整字符串':' 标志。这是可能的,还是只有数字?
  • @Bubbly 这是可能的。正则表达式是一个非常强大的工具。您可以使用匹配的startend 方法获取索引。如果你想找到其他字母,那就是另一个问题了。
【解决方案2】:

如果您想使用单行正则表达式方法,则将String#replaceAll 与此正则表达式模式一起使用:

\D*\d+\D+(\d+).*

这将在第一个捕获组中捕获输入中的第二个数字。示例脚本:

String input = "1 guacamole 36";
String secondNum = input.replaceAll("\\D*\\d+\\D+(\\d+).*", "$1");
System.out.println(secondNum);  // prints 36

注意:如果输入 not 中至少有两个单独的数字,那么上面的替换实际上只会返回输入。在这种情况下,如果您想报告不匹配,则应首先使用 String#matches 检查匹配模式。

【讨论】:

    【解决方案3】:

    我认为只要字符串包含它并且您想要处理,您就需要继续查看下一个项目。如其他答案中所述,如果您明确寻找第二个号码,那么您可以使用regex 或使用second.group(1)

    如果您正在寻找字符串中给出的最小和最大数字,这就是我所做的:

    String text = "1 guacamole 36 admin test 23 which is equivalent 22";
    Pattern pattern = Pattern.compile("\\d+");
    Matcher matcher = pattern.matcher(text);
    List<Integer> numbers = new ArrayList<>();
    while (matcher.find()) {
        numbers.add(Integer.parseInt(matcher.group()));
    }
    System.out.println(numbers);
    Collections.sort(numbers);
    int min = -1;
    int max = -1;
    if (!numbers.isEmpty()) {
        int size = numbers.size();
        min = numbers.get(0);
        max = numbers.get(size - 1);
    }
    System.out.println("Min : " + min);
    System.out.println("Max : " + max);
    

    这应该打印出来

    [1, 36, 23, 22]
    Min : 1
    Max : 36
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      相关资源
      最近更新 更多