我试图用这种形式简单地获取数字 28
integer+space+integer+integer+space+integer我试过这个正则表达式 \\s\\d\\d\\s
但我得到了两个数字11
和 28
使用此表达式 \\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)");
这不需要任何空格修剪。
(?<=\\d\\s)
正后视断言匹配前必须有一个数字和一个空格。\d{2}
刚好是两位数。(?=\\s\\d)
断言匹配的数字后必须跟一个空格和一个数字。
关于java - 使用此表单获取号码\d\s\d\d\s\d,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30692278/