简体   繁体   English

用Map中的RegEx键替换所有出现的String

[英]Replacing all occurrences of a String with RegEx keys from a Map

I have String like below, 我有像下面的字符串,

{Account={type=object,id={type=integer, format=int64}, enum=[ENABLED, DISABLED]}, token={type=string}}}, AssetPlayerRequest={type=object, properties={assetPlayer={description=The asset player details, {$ref=#/definitions/AssetPlayer},ImageFile={type=object, properties={description={type=string}, filePath={type=string}, fileUri={$ref=#/definitions/URI}}} {Account = {type = object,id = {type = integer,format = int64},enum = [ENABLED,DISABLED]},token = {type = string}}},AssetPlayerRequest = {type = object,properties = {assetPlayer = {description =资产者详细信息,{$ ref =#/ definitions / AssetPlayer},ImageFile = {type = object,properties = {description = {type = string},filePath = {type = string},fileUri = {$ REF =#/定义/ URI}}}

I want to replace the RegEx patterns like {$ref=#/definitions/URI} , {$ref=#/definitions/AssetPlayer} with the values available in a Map with Keys like URI , AssetPlayer which are part of RegEx itself. 我想将{$ref=#/definitions/URI}{$ref=#/definitions/AssetPlayer}等RegEx模式替换为带有像URIAssetPlayer等密钥的Map中可用的值,它们是RegEx本身的一部分。

How to achieve this in Java 8? 如何在Java 8中实现这一点?

Partially adapted from Matcher.replaceAll() : 部分改编自Matcher.replaceAll()

public static void main(String[] args) {
    Map<String, String> replacements = new HashMap<>();
    replacements.put("AssetPlayer", "replacement1");
    replacements.put("URI", "replacement2");

    String input = "{Account={type=object,id={type=integer, format=int64}, enum=[ENABLED, DISABLED]}, token={type=string}}}, AssetPlayerRequest={type=object, properties={assetPlayer={description=The asset player details, {$ref=#/definitions/AssetPlayer},ImageFile={type=object, properties={description={type=string}, filePath={type=string}, fileUri={$ref=#/definitions/URI}}}";

    Matcher matcher = Pattern.compile("\\{\\$ref=#\\/definitions\\/(.+?)\\}").matcher(input);
    StringBuffer sb = new StringBuffer();
    if (matcher.find()) {
        do {
            String replacement = replacements.get(matcher.group(1));
            matcher.appendReplacement(sb, Matcher.quoteReplacement(replacement));
        } while (matcher.find());
        matcher.appendTail(sb);
        System.out.println(sb.toString());
    } else {
        System.out.println(input);
    }
}

Output: 输出:

{Account={type=object,id={type=integer, format=int64}, enum=[ENABLED, DISABLED]}, token={type=string}}}, AssetPlayerRequest={type=object, properties={assetPlayer={description=The asset player details, replacement1,ImageFile={type=object, properties={description={type=string}, filePath={type=string}, fileUri=replacement2}}

The general formula for replacing things in a string that requires more logic is in Matcher#appendReplacement : 替换需要更多逻辑的字符串中的东西的通用公式在Matcher#appendReplacement

public String bindTemplateVariables(String input) {
    Pattern p = Pattern.compile("\\{\\$ref=#/definitions/(.*?)\\}");
    Matcher m = p.matcher(input);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        String replacement = map.get(m.group(1));
        m.appendReplacement(sb, replacement);
    }
    m.appendTail(sb);
    return sb.toString();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM