package com.li.project.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author ex_limengzhen
 * @description 一些自用正则工具类,啥时候有用到新的了再更新
 * @createDate 2023/8/23
 */
public class MatcherUtil {

    /**
     * 根据指定正则在字符串中找寻匹配的字符串并返回匹配字符串集合
     * @param reg 正则表达式
     * @param toFindStr 要查找的字符串
     * @return 匹配的字符传集合
     */
    public static List<String> matcherFindString(String reg, String toFindStr) {
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(toFindStr);
        List<String> resultStr = new ArrayList<>();
        while (matcher.find()) {
            resultStr.add(matcher.group());
        }
        return resultStr;
    }

    /**
     * 将字符串中Unicode编码字符转换成正常utf-8的格式
     * @param sourceStr 给定字符串
     * @return 转换之后的字符传
     */
    public static String parseUnicodeToString(String sourceStr) {
        StringBuilder buffer = new StringBuilder();
        Pattern compile = Pattern.compile("\\\\u([0-9a-fA-F]{4})");
        Matcher matcher = compile.matcher(sourceStr);
        while (matcher.find()) {
            String matchedStr = matcher.group(1);
            int number = Integer.parseInt(matchedStr, 16);
            String word = Character.toString((char) number);
            matcher.appendReplacement(buffer, word);
        }
        matcher.appendTail(buffer);
        return buffer.toString();
    }

}