Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java字符串用求值键替换正则表达式模式_Java_Regex - Fatal编程技术网

Java字符串用求值键替换正则表达式模式

Java字符串用求值键替换正则表达式模式,java,regex,Java,Regex,我有一个这样的输入字符串 我想去发生{事情}的{地方}。 {places}和{things}的值是延迟计算的(即,首先我找出所有键需要替换的内容,然后计算它们的值,然后在原始字符串中替换它们) 我能够找到所有的钥匙,并删除他们使用下面的代码 public class Temp { private static final Pattern betweenCurlyBracesMatcher = Pattern.compile("\\{(.*?)\\}"); public stat

我有一个这样的输入字符串

我想去发生{事情}的{地方}。

{places}和{things}的值是延迟计算的(即,首先我找出所有键需要替换的内容,然后计算它们的值,然后在原始字符串中替换它们)

我能够找到所有的钥匙,并删除他们使用下面的代码

public class Temp {
    private static final Pattern betweenCurlyBracesMatcher = Pattern.compile("\\{(.*?)\\}");

    public static void main(String args[]) {
        System.out.println(resolve2("hello {world} from {here}"));
    }

    public static String resolve2(String input) {
        Map<String, String> keyValueMap = new HashMap<>();
        Matcher matcher = betweenCurlyBracesMatcher.matcher(input);
        while (matcher.find()) {
            String key = matcher.group(1);
            if (!keyValueMap.containsKey(key)) {
                keyValueMap.put(key, computeValueForKey(key));
            }
        }
        for (Map.Entry<String, String> entry : keyValueMap.entrySet()) {
            input = input.replace("{" + entry.getKey() + "}", entry.getValue());  // << ugly code here
        }
        return input;
    }

    private static String computeValueForKey(String key) {
        return "new" + key;
    }
}
因为这意味着每当我改变我的正则表达式时,我都必须更新这个逻辑。这个问题有没有更优雅的解决方案


输入你好{world}来自{here}

输出您好,来自newhere的新世界


input我想去发生{事情}的{地方}


输出我想去新事物发生的地方。

您应该在这里使用
Matcher#appendReplacement
Matcher#appendTail
API:

Map<String, String> keyValueMap = new HashMap<>();
keyValueMap.put("places", "to America");
keyValueMap.put("things", "events");
String input = "I want to go to {places} where {things} are happening.";
Pattern pattern = Pattern.compile("\\{(.*?)\\}");
Matcher matcher = pattern.matcher(input);
StringBuffer buffer = new StringBuffer();

while(matcher.find()) {
    matcher.appendReplacement(buffer, keyValueMap.get(matcher.group(1)));
}
matcher.appendTail(buffer);
System.out.println(buffer.toString());
我们可以使用matcher.group(0)进行返回

公共类临时{
私有静态最终模式betweenCurlyBracesMatcher=Pattern.compile(“\\{(.*?\\}”);
公共静态void main(字符串参数[]){
System.out.println(resolve2(“hello{world}from{here}”);
}
公共静态字符串解析2(字符串输入){
Map keyValueMap=新HashMap();
Matcher Matcher=betweenCurlyBracesMatcher.Matcher(输入);
while(matcher.find()){
字符串keyBetweenBraces=matcher.group(1);
字符串keyWithBraces=matcher.group(0);
如果(!keyValueMap.containsKey(带大括号的键)){
keyValueMap.put(带括号的键,计算值forkey(keyBetweenBraces));
}
}
对于(Map.Entry:keyValueMap.entrySet()){
input=input.replace(entry.getKey(),entry.getValue());
}
返回输入;
}
私有静态字符串computeValueForKey(字符串键){
返回“new”+键;
}
}

您的实际预期输出是什么?@TimBiegeleisen更新了这个问题。一个简短的建议:Spring生态系统和其他框架为属性占位符提供了方法,这些方法与您使用正则表达式实现的方法相同。这不是解决问题的理想方法。对原始字符串迭代一次以查找所有匹配项,然后对每个匹配项进行第二次迭代以进行替换。这是
N^2
behavior。是的,我正在研究StrSubstitutor.replace(“heelo-hi-ok{ok}”,keyValueMap)从apache一步完成。这里没有apache库;核心Java会做得很好,请看我的答案。
Map<String, String> keyValueMap = new HashMap<>();
keyValueMap.put("places", "to America");
keyValueMap.put("things", "events");
String input = "I want to go to {places} where {things} are happening.";
Pattern pattern = Pattern.compile("\\{(.*?)\\}");
Matcher matcher = pattern.matcher(input);
StringBuffer buffer = new StringBuffer();

while(matcher.find()) {
    matcher.appendReplacement(buffer, keyValueMap.get(matcher.group(1)));
}
matcher.appendTail(buffer);
System.out.println(buffer.toString());
I want to go to to America where events are happening.
public class Temp {
    private static final Pattern betweenCurlyBracesMatcher = Pattern.compile("\\{(.*?)\\}");

    public static void main(String args[]) {
        System.out.println(resolve2("hello {world} from {here}"));
    }

    public static String resolve2(String input) {
        Map<String, String> keyValueMap = new HashMap<>();
        Matcher matcher = betweenCurlyBracesMatcher.matcher(input);
        while (matcher.find()) {
            String keyBetweenBraces = matcher.group(1);
            String keyWithBraces = matcher.group(0);
            if (!keyValueMap.containsKey(keyWithBraces)) {
                keyValueMap.put(keyWithBraces, computeValueForKey(keyBetweenBraces));
            }
        }
        for (Map.Entry<String, String> entry : keyValueMap.entrySet()) {
            input = input.replace(entry.getKey(), entry.getValue());
        }
        return input;
    }

    private static String computeValueForKey(String key) {
        return "new" + key;
    }
}