"中的内容,并且存入hashmap,用正则表达式做 ...">
美文网首页
正则表达式提取尖括号中内容,存入HashMap

正则表达式提取尖括号中内容,存入HashMap

作者: Ariel_Tian | 来源:发表于2016-10-24 22:27 被阅读778次

需求:

提取"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>
运行结果:

Paste_Image.png

相关文章

网友评论

      本文标题:正则表达式提取尖括号中内容,存入HashMap

      本文链接:https://www.haomeiwen.com/subject/yaaluttx.html