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

需求:

提取"LR(:12970100:)"字符串中"<>"中的内容,并且存入hashmap,用正则表达式做

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:)";
    String line_2 = "LR(:12970100:)";

    Pattern p = Pattern.compile("<(.*?)>");
    Matcher m = p.matcher(line_1);
    int size = 0;
    List> mList = new ArrayList<>();
    while (m.find()) {
        size++;
        System.out.println(m.group(1));
        HashMap 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));
    }
}

}


运行结果:

正则表达式提取尖括号中内容,存入HashMap_第1张图片
Paste_Image.png

你可能感兴趣的:(正则表达式提取尖括号中内容,存入HashMap)