有这样一个数组:
[{0=1}, {0=2}, {0=3}, {0=4}, {0=5}, {0=6}, {0=7}, {0=8}, {0=9}, {0=10}, {0=11}, {0=12}, {0=13}, {0=14}, {0=15}, {0=16}, {0=17}, {0=18}, {0=19}]
现在只想要[1,2,3,4...]要怎么方便取出来啊???
List<String> collect = arr.stream().map(x->x.split("=")[1]).collect(Collectors.toList());
//利用正则表达式截取=号后面的值
Pattern p = Pattern.compile("\\{.+=(.+)\\}");
List<String> collect = Stream.of(new String[]{"{0=1}", "{0=12}", "{0=123}", "{1=1234}", "{1=1245}"}).map((s) -> {
Matcher matcher = p.matcher(s);
//group(1)表示正则表达式中第一对括号里的值
return matcher.find()?matcher.group(1):null;
}).collect(Collectors.toList());
//打印结果
collect.stream().forEach(System.out::println);
public class TestDemo {
public static void main(String args[]) {
List list = new ArrayList();
String string = "[{0=1}, {0=2}, {0=3}, {0=4}, {0=5}, {0=6}, {0=7}, {0=8}, {0=9}, {0=10}, {0=11}, {0=12}," +
" {0=13}, {0=14}, {0=15}, {0=16}, {0=17}, {0=18}, {0=19}]";
Pattern pattern = Pattern.compile("\\{.*=[0-9]+\\}");
// 这个是这么匹配的 。。。。
Matcher matcher = pattern.matcher(string);
if(matcher.find()){
String group = matcher.group();
String[] split = group.split(",");
for(int i =0;i<split.length;i++){
split[i] = split[i].replace("}","").trim();
list.add(split[i].substring(3));
//这个3 是因为你前面都是{0=所以直接这样写的
}
}
System.out.println(list);
}
}
java的正则感觉好难用。