首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用映射中的RegEx键替换字符串的所有出现

用映射中的RegEx键替换字符串的所有出现
EN

Stack Overflow用户
提问于 2017-01-03 19:37:02
回答 2查看 427关注 0票数 3

我的绳子跟下面一样,

{Account={type=object,id={type=integer,format=int64},enum=ENABLED,format=int64}},token={type=string}},AssetPlayerRequest={type=object,properties={assetPlayer={description=The资产播放器详细信息,{$ref=#/ details /AssetPlayer},ImageFile={type=object,properties={description={type=object},description={en19#},{$ref=#/details/URI}}

我想用地图中可用的值替换RegEx模式,如{$ref=#/definitions/URI}{$ref=#/definitions/AssetPlayer},并使用URIAssetPlayer等作为RegEx本身一部分的键。

如何在Java 8中实现这一点?

EN

Stack Overflow用户

发布于 2017-01-03 19:52:36

在需要更多逻辑的字符串中替换事物的一般公式在Matcher#appendReplacement中。

代码语言:javascript
复制
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();
}
票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41451063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档