正则表达式替换占位符

解析占位符

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

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);
    }
}
相关推荐
钢铁男儿3 天前
Python 正则表达式(正则表达式和Python 语言)
python·mysql·正则表达式
钢铁男儿3 天前
Python 正则表达式实战:解析系统登录与进程信息
开发语言·python·正则表达式
拾心213 天前
【运维进阶】Linux 正则表达式
linux·运维·正则表达式
莲动渔舟3 天前
第4.3节:awk正则表达式详解-特殊字符
正则表达式·编程语言·awk
G_H_S_3_3 天前
【网络运维】Linux:正则表达式
linux·运维·网络·正则表达式
yuxb733 天前
Linux 文本处理与 Shell 编程笔记:正则表达式、sed、awk 与变量脚本
linux·笔记·正则表达式
烟锁池塘柳04 天前
【R语言】R 语言中 gsub 与正则表达式详解(含 POSIX 与 Perl 风格实例)
正则表达式·r语言·perl
郝学胜-神的一滴7 天前
基于C++的词法分析器:使用正则表达式的实现
开发语言·c++·程序人生·正则表达式·stl
ruleslol8 天前
python30-正则表达式
python·正则表达式
Big Cabbage9 天前
python 正则表达式
python·正则表达式