开发者社区> 问答> 正文

在Java中如何选择最接近的匹配字符串

我有这样的JSON。

{
    "mappingData": {
        "input.data.DT": "Integer",
        "input.data.DTF": "STRING"
    }
}

和这样的嵌套函数。

Functions.test2(Functions.test1(Functions.test3(input.data.DTF),input.data.DT,"string","int"),input.data.DTF)

现在我想用相应函数位置的值替换mappingdata json或map键中的键。我该如何使用好像包含和替换,而不是将其替换为mappingData键input.data.DTF,与input.data.DT类似,因此如何执行,所以替换将不一致。我怎样才能做到这一点。

预期结果:

Functions.test2(Functions.test1(Functions.test3(STRING),Functions.test2(Functions.test1(Functions.test3(STRING),Integer,"string","int"),STRING),"string","int"),STRING)

JAVA代码:

Map<String, String> mapData = (Map<String, String>) data.get("mappingData");
System.err.println("mapData===>" + mapData);
for (Map.Entry<String, String> entry : map.entrySet()) {
  try {
    String function = entry.getValue();
    //System.out.println("function===="+function);
    if (function.contains("Functions")) {
      for (Entry<String, String> entry1 : mapData.entrySet()) {
        if (function.contains(entry1.getKey())) {
          function = function.replace(entry1.getKey(), entry1.getValue());
        }
      }
    }
  }
}

展开
收起
几许相思几点泪 2019-12-16 20:29:44 839 0
1 条回答
写回答
取消 提交回答
  • java正则提取需要用到Matcher类,下面给出案例示例供参考 需要提取车牌号中最后一个数字,比如说:苏A7865提取5,苏A876X提取6

    import java.util.regex.Matcher;import java.util.regex.Pattern;public class Test {public static void main(String[] args) {String s = "A876X";// 把要匹配的字符串写成正则表达式,然后要提取的字符使用括号括起来// 在这里,我们要提取最后一个数字,正则规则就是“一个数字加上大于等于0个非数字再加上结束符”Pattern pattern = Pattern.compile("(\d)[^\d]*$");Matcher matcher = pattern.matcher(s);if(matcher.find())System.out.println(matcher.group(1));}}

    关于Matcher 中的几个方法说明: Mathcer.start() Matcher.end() Matcher.group() 当使用matches(),lookingAt(),find()执行匹配操作后,就可以利用以上三个方法得到更详细的信息. start()返回匹配到的子字符串在字符串中的索引位置. end()返回匹配到的子字符串的最后一个字符在字符串中的索引位置. group()返回匹配到的子字符串 示例代码如下,具体功能请参考注释

    Pattern p=Pattern.compile(“\d+”); Matcher m=p.matcher(“aaa2223bb”); m.find();//匹配2223 m.start();//返回3 m.end();//返回7,返回的是2223后的索引号 m.group();//返回2223Mathcer m2=p.matcher(“2223bb”); m2.lookingAt(); //匹配2223 m2.start(); //返回0,由于lookingAt()只能匹配前面的字符串,所以当使用lookingAt()匹配时,start()方法总是返回0 m2.end(); //返回4 m2.group(); //返回2223Matcher m3=p.matcher(“2223”); //如果Matcher m3=p.matcher(“2223bb”); 那么下面的方法出错,因为不匹配返回false m3.matches(); //匹配整个字符串 m3.start(); //返回0 m3.end(); //返回3,原因相信大家也清楚了,因为matches()需要匹配所有字符串 m3.group(); //返回2223

    另外,Mathcer类中start(),end(),group()均有一个重载方法它们是start(int i),end(int i),group(int i)专用于分组操作,Mathcer类还有一个groupCount()用于返回有多少组. 示例如下:

    Pattern p=Pattern.compile(“([a-z]+)(\d+)”); Matcher m=p.matcher(“aaa2223bb”); m.find(); //匹配aaa2223 m.groupCount(); //返回2,因为有2组 m.start(1); //返回0 返回第一组匹配到的子字符串在字符串中的索引号 m.start(2); //返回3 m.end(1); //返回3 返回第一组匹配到的子字符串的最后一个字符在字符串中的索引位置. m.end(2); //返回7 m.group(1); //返回aaa,返回第一组匹配到的子字符串 m.group(2); //返回2223,返回第二组匹配到的子字符串

    注意: 只有当匹配操作成功,才可以使用start(),end(),group()三个方法,否则会抛出java.lang.IllegalStateException,也就是当matches(),lookingAt(),find()其中任意一个方法返回true时,才可以使用。

    2021-02-09 11:02:55
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载