//正则 匹配[]中的数据
String line = "欠费:[5.31元]+[2022-11-14 11:38:14]";
String pattern = "\\[(.*?)\\]";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
List str = new ArrayList();
while(m.find()) {
System.out.println(m.group());
str.add(m.group());
}
System.out.println("Found value: " +str.get(0) );
String mouthStr = String.valueOf(str.get(0));
String mouth = mouthStr.substring(1,mouthStr.length()-2);
System.out.println(mouth);
输出
[5.31元]
[2022-11-14 11:38:14]
Found value: [5.31元]
5.31