正则表达式替换占位符

解析占位符

如何从下面的一个字符串中解析除占位符,并根据变量的类型替换。

java 复制代码
"(<{AMT_field_value,string}> == nil ? false : string.contains( <{AMT_rule_value,string}>, <{AMT_field_value,array}>))

代码如下

java 复制代码
import org.apache.flink.api.java.tuple.Tuple2;

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

/**
 * @className: ExpressionParser
 * @Description:
 * @Author: wangyifei
 * @Date: 2023/10/11 21:45
 */
public class ExpressionParser {
    public static void main(String[] args) {
        String str = "(<{AMT_field_value,string}> == nil ? false : string.contains( <{AMT_rule_value,string}>, <{AMT_field_value,array}>))";
        Pattern p = Pattern.compile("<\\{(.*?)\\}>");
        Matcher matcher = p.matcher(str);
        String find = "";
        String[] split = null ;
        String key = null;
        String value = null ;
        String replace = str ;
        Map<String,Object> parameter = new HashMap<>();
        parameter.put("AMT_field_value","vvvv");
        while (matcher.find()) {
            find = matcher.group(1);
            split = find.split("\\s*,\\s*");
            key = split[0];
            value = split[1];
            replace = replace.replace("<{"+find+"}>" , String.valueOf(parameter.get(key)));
        }
        System.out.println(replace);
    }
}
相关推荐
ths51218 小时前
Python 正则表达式实战指南:从入门到精通(12 个高频案例)(三)
python·正则表达式
ths51219 小时前
Python 正则表达式学习笔记(小白超详细版)(一)
python·正则表达式
kcuwu.4 天前
Python 正则表达式从入门到实战
数据库·python·正则表达式
羊小蜜.4 天前
Mysql 07: 正则表达式查询(REGEXP)全解
数据库·mysql·正则表达式
Dxy12393102164 天前
正则表达式如何匹配提取文章日期
数据库·mysql·正则表达式
Dxy12393102167 天前
Python正则表达式判断姓名:详细解析
python·mysql·正则表达式
一个小猴子`8 天前
快速了解正则表达式
正则表达式
禾小西8 天前
Java中使用正则表达式核心解析
java·python·正则表达式
Amumu121388 天前
Js:正则表达式(一)
开发语言·javascript·正则表达式
榴莲omega9 天前
正则表达式入门与实战指南
javascript·正则表达式·js八股