正则表达式之字符串替换

65 阅读3分钟

查询字符串中是否包含某个字符

例如:查询字符串中是否包含i18n{}字样

public static final String I18N_REGEX = "i18n\{(.+?)\}";

/**
 * @param message
 * @throws
 * @description 查询字符串中是否包含i18n字样
 * @createTime 2023-12-07 22:22:45
 */
public static boolean i18nRegexMatcher(String message) {
    if (StringUtils.isBlank(message)) {
        return false;
    }

    Pattern pattern = Pattern.compile(I18N_REGEX);
    Matcher matcher = pattern.matcher(message);
    return matcher.matches();
}

提取字符串中含有特殊字符的内容

例如:提取下方json字符串中含有i18n{}字符的数据

{
    "title": "i18n{1733020778247766016}",
    "search": {
        "file": [
            {
                "uid": "vc-upload-1693908227500-54",
                "url": "/home/data/nas/3/record/115/202309/05/20230905194323589001.json",
                "name": "网关.json"
            }
        ],
        "countdown": 60
    },
    "addDirectDeviceSwitch": true,
    "addDirectDevice": {
        "content": "i18n{1732957662684135425}",
        "pageId": "42f3cd9d-3b91-48f8-bedb-77335c0dbb5e"
    },
    "remindSwitch": true,
    "remind": {
        "content": "i18n{1732957662684135426}"
    },
    "buttonsSwitch": true,
    "buttons": [
        {
            "name": "i18n{1732957662684135421}",
            "pageId": "05a4a7b4-89d7-42bd-a219-340968c76b78"
        }
    ],
    "autoJumpSwitch": false
}
/**
 * @param message
 * @throws
 * @description 提取i18n字符的数据
 * @createTime 2023-12-11 10:07:51
 */
public static List<String> extractI18nStr(String message) {
    List<String> i18nList = new ArrayList<>();
    Pattern pattern = Pattern.compile(I18nRegexExam.I18N_REGEX);
    Matcher matcher = pattern.matcher(message);
    int account = 0;
    while (matcher.find()) {
        //避免产生死循环
        if (account > 100) {
            break;
        }
        i18nList.add(matcher.group());
        account++;
    }
    return i18nList;
}

替换字符串中含有特殊字符的内容

例如:替换下方json字符串中含有i18n{}字符的数据

{
    "title": "i18n{1733020778247766016}",
    "search": {
        "file": [
            {
                "uid": "vc-upload-1693908227500-54",
                "url": "/home/data/nas/3/record/115/202309/05/20230905194323589001.json",
                "name": "网关.json"
            }
        ],
        "countdown": 60
    },
    "addDirectDeviceSwitch": true,
    "addDirectDevice": {
        "content": "i18n{1732957662684135425}",
        "pageId": "42f3cd9d-3b91-48f8-bedb-77335c0dbb5e"
    },
    "remindSwitch": true,
    "remind": {
        "content": "i18n{1732957662684135426}"
    },
    "buttonsSwitch": true,
    "buttons": [
        {
            "name": "i18n{1732957662684135421}",
            "pageId": "05a4a7b4-89d7-42bd-a219-340968c76b78"
        }
    ],
    "autoJumpSwitch": false
}

/**
 * @param template 需要被替换的字符串
 * @param data     需要替换的map,其他key:为被替换的特殊字符,value:为要替换的值
 * @throws
 * @description 替换字符串中的特殊字符   
 * @createTime 2023-12-19 09:18:46
 */

public static String composeMessage(String template, Map data) {
    if (CollectionUtils.isEmpty(data) || StringUtils.isBlank(template)) {
        return template;
    }
    Pattern pattern = Pattern.compile(I18N_REGEX);
    Matcher matcher = pattern.matcher(template);
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        String name = matcher.group(0);//键名
        String value = (String) data.get(name);//键值
        if (value == null) {
            logger.warn("i18n转换失败,需要转换的i18nKey=[{}]对应的值value=[{}]为空,直接跳过", name, value);
            value = "";
        } else {
            value = value.replaceAll("\$", "\\\$");
        }
        matcher.appendReplacement(sb, value);
    }
    //最后还得要把尾串接到已替换的内容后面去,这里尾串为“,欢迎下次光临!”
    matcher.appendTail(sb);
    return sb.toString();
}