我的输入字符串可以是以下形式
"cust_100dept_200_address_300";
"cust_100_dept_200_address_300";
"dept_200_address_300cust_100";
"address_300cust_100dept_200";
基本上有三个属性,即 cust、dept 和 address,每个属性后跟下划线和一些数字。正如我在我的示例中显示的那样,顺序很灵活,其中 cust_100 可以出现在开头或中间或最后。
我想要每个属性的数字(即下划线之后)。所以我的预期输出(无论输入属性的顺序是什么)是
group1 = 100
group2 = 200
group3 = 300
我在下面试过
Pattern p = Pattern.compile(
"cust_(\\d+)" +
"dept_(\\d+)" + "address_(\\d+)");
Matcher m = p.matcher(input);// where input can be anything i stated in the beginning
if (m.find()) {
System.out.println("inside while");
System.out.println("group1 = " + m.group(1));
System.out.println("group2" + m.group(2));
System.out.println("group2" + m.group(3));
}
But i am not getting desired output?