当我知道格式始终为 ${INT1...INT2}
例如,从字符串中期望 2 个整数的最简单的简洁方法是什么? “您好${123...456}
会提取123,456
吗?
最佳答案
我会选择带有组和反向引用的模式
。
这是一个例子:
String input = "Hello ${123...456}, bye ${789...101112}";
// | escaped "$"
// | | escaped "{"
// | | | first group (any number of digits)
// | | | | 3 escaped dots
// | | | | | second group (same as 1st)
// | | | | | | escaped "}"
Pattern p = Pattern.compile("\\$\\{(\\d+)\\.{3}(\\d+)\\}");
Matcher m = p.matcher(input);
// iterating over matcher's find for multiple matches
while (m.find()) {
System.out.println("Found...");
System.out.println("\t" + m.group(1));
System.out.println("\t" + m.group(2));
}
输出
Found...
123
456
Found...
789
101112
关于java - 在Java中匹配${123...456}并提取2个数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23538072/