我有一个包含键和值的字符串:
A=1,B=2,C=3,D=4,E=5
我需要在java中使用split regex从字符串上面获取这些值(1,2,3,4,5)。
答案 0 :(得分:1)
您可以使用此模式 [0-9] +
的正则表达式 String toIndex = "A=1,B=2,C=3,D=4,E=5";
Pattern p = Pattern.compile("[0-9]+");
Matcher m = p.matcher(toIndex);
while (m.find()) {
System.out.println(m.group());
}
并且在while中而不是打印组然后添加到列表或类似的后面的操作
答案 1 :(得分:1)
使用它:
String s = "A=1,B=2,C=3,D=4,E=5";
Pattern p = Pattern.compile("=(\\d+)");
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group(1));
}
\ d +是一个直到n位数
答案 2 :(得分:0)
由于您要求使用拆分,这也可以。
String str2 = "A=1,B=2,C=3,D=4,E=5";
String [] between = str2.split("=+|,");
for(int i=1; i<between.length; i+=2){
System.out.println(between[i]);
}
适用于=和
之间的字符串和数字(A = AA,B = BB,C = CC,d = DD,E = EE)