需求:
提取"LR(:12970100:<sort:weight-reatime-desc><size:5>)"字符串中"<>"中的内容,并且存入hashmap,用正则表达式做
<pre>package com.company;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
// write your code here
String line_1 = "L1(A:12970000:<scoring_id:97><size:5><sort:weight-reatime-desc><size:5><sort:weight-reatime-desc><size:5>)";
String line_2 = "LR(:12970100:<sort:weight-reatime-desc><size:5>)";
Pattern p = Pattern.compile("<(.*?)>");
Matcher m = p.matcher(line_1);
int size = 0;
List<HashMap<String,String>> mList = new ArrayList<>();
while (m.find()) {
size++;
System.out.println(m.group(1));
HashMap<String, String> map = new HashMap<>();
String[] arr = m.group(1).split(":");
map.put(arr[0], arr[1]);
mList.add(map);
}
System.out.println("-------------------------------------------");
for (int i = 0; i < size; i++) {
System.out.println(mList.get(i));
}
}
}
</pre>
运行结果:
网友评论