我的正则表达式有来自同一代码的 2 个不同的输出...但我不知道出了什么问题。这是一段代码,希望你能帮助我。谢谢!
String s = "48° 18′ 13,94″ nördliche Breite, "
+ "11° 34′ 31,98″ östliche Länge";
String kommazahl = "[0-9]{1,2}([\\.,][0-9]+)?";
String zahl = "[0-9]{1,2}";
Pattern p1 = Pattern.compile("("+ zahl +"[°/| ]{1,2}"+ zahl +"(['′/| ]{1,2}("+ kommazahl +")?)?).*"
+"("+ zahl +"[°/| ]{1,2}"+ zahl +"(['′/| ]{1,2}("+ kommazahl +")?)?).*");
Matcher m1 = p1.matcher(s);
System.out.println(m1.group(1) + "\n" + m1.group(5));
// Output should be:
// 48° 18′ 13,94
// 11° 34′ 31,98
// Output is:
// 48° 18′ 13,94
// 1° 34′ 31,98
最佳答案
.*
贪婪地匹配 11 个模式中的前 1 个,同时仍然允许模式的其余部分匹配。将 .*
替换为 [^0-9]*
。
关于Java 错误? 2 个相同正则表达式的不同输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/955796/