正则表达式替换占位符

解析占位符

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

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);
    }
}
相关推荐
qq_251533595 小时前
如何使用 Python 正则表达式去除空格/制表符/换行符?
开发语言·python·正则表达式
代码续发5 小时前
正则表达式小记
正则表达式
GISer_Jing1 天前
Showdown解析策略:正则替换的利与弊
前端·正则表达式
BD_Marathon1 天前
【JavaWeb】JS_了解正则表达式
正则表达式
十一.3662 天前
85-90 正则表达式的简介,正则语法,字符串和正则相关的方法,正则表达式语法
正则表达式
刺客xs2 天前
MySQL数据库----通配符,正则表达式
数据库·mysql·正则表达式
Cherry的跨界思维2 天前
8、Python正则表达式精准搜索实战:从模糊匹配到精准定位
开发语言·python·正则表达式·django·flask·pygame·tornado
云计算老刘5 天前
3.Shell 变量基础知识
chrome·正则表达式·centos·云计算
F***E2396 天前
SQL中的REGEXP正则表达式使用指南
数据库·sql·正则表达式
Doris8936 天前
【JS】Web APIs BOM与正则表达式详解
前端·javascript·正则表达式