调用返回报文标签中的存在中划线“-”,不符合规范,需要统一进行转换,但不能替换标签内的内容,利用正则匹配重新输出

/**
* 正则匹配报文中的xml标签,将其中的"-"转换为"_",重新输出
* 示例<Card-No>120-111</Card-No>
* 转换输出为:<Card_No>120-111</Card_No>
*/
public static String replaceMidLine(String resourceXml){
    /**
    * 匹配规则:
    * <[^>]+>:匹配xml(html)标签,如<a>,</a>;<a/>;< > 内的除 > 之外的所有字符
    * (?=.*[-]):匹配含 - 的字符串,与上条规则并列
    *
    */
    Pattern pattern = Pattern.compile("<(?=.*[-])[^>]+>");
    Matcher matcher = pattern.matcher(resourceXml);
    StringBuffer sb = new StringBuffer();
    while(matcher.find()){
        matcher.appendReplacement(sb,matcher.group().replace("-","_"));
    }
    matcher.appendTail(sb);
    return sb.toString();
}

 

相关文章:

  • 2021-06-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-28
  • 2021-07-22
  • 2022-12-23
猜你喜欢
  • 2021-05-28
  • 2021-05-18
  • 2021-11-25
  • 2022-02-10
  • 2021-09-24
  • 2021-09-29
  • 2021-10-06
相关资源
相似解决方案