gpt4 book ai didi

java - 使用此表单获取号码\d\s\d\d\s\d

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:09:36 26 4
gpt4 key购买 nike

我试图用这种形式简单地获取数字 28 integer+space+integer+integer+space+integer我试过这个正则表达式 \\s\\d\\d\\s 但我得到了两个数字1128

使用此表达式 \\d\\s\\d\\d\\s\\d 我收到此错误 java.lang.NumberFormatException: For input string: "4 60 1"

数字不应采用字母+空格+整数+整数+空格的形式。

我该如何解决?

简单:

ZOB/Hauptbahnhof Bussteig 11 20:04 20:34 28 21:08 21:40 22:08 22:40 23:08 23:40 00:30

代码:

Pattern pattern = Pattern.compile("\\s\\d\\d\\s");
//Pattern pattern = Pattern.compile("\\d\\s\\d\\d\\s\\d");

Matcher m = pattern.matcher(line);
while (m.find()) {
value = Integer.parseInt(m.group().trim());
if (value != 10) {
line = line.replace(m.group(), " ").replaceAll(" +", " ");
writer.println("Min:" + value);

// String line3 = scanner.nextLine();

System.out.println(value
+ " has been found in this text document " + newName);
}

}

最佳答案

您需要使用环视。

Pattern pattern = Pattern.compile("(?<=\\d\\s)\\d{2}(?=\\s\\d)");

这不需要任何空格修剪。

DEMO

  • (?<=\\d\\s)正后视断言匹配前必须有一个数字和一个空格。

  • \d{2}刚好是两位数。

  • (?=\\s\\d)断言匹配的数字后必须跟一个空格和一个数字。

关于java - 使用此表单获取号码\d\s\d\d\s\d,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30692278/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com