如何更改 java 中标签之间的内容?

How do I change the content between tags in java?

我正在寻找一个正则表达式,我可以在其中查找 FIX 中的某些标签,然后向它们添加信息。 我想将标签作为字符串引用,并使用 ReplaceAll 方法在正则表达式中搜索它。但是,当我不想替换正则表达式中的字符串而是替换它后面的字符串时,我不知道该怎么做。(意思是标签中的内容)。

FIX protocol body is a set of name=value pairs separated by SOH (0x01) characters, and you want to replace the value, you can use zero-width positive lookbehind 模式以来。

示例:替换标签 49

的值
body = body.replaceAll("(?<=\u000149=)[^\u0001]*",
                       Matcher.quoteReplacement("new value"));

说明

(?<=           The matched value must be preceded by:  
  \u0001         SOH field delimiter
  49             Tag number
  =              Separating equal sign
)
[^\u0001]*     Matches value, e.g. everything up to the following SOH field delimiter

如果您需要添加文本 before/after/around 现有测试,只需插入匹配的文本,使用 [=15=] 引用,这也意味着您不应该使用 quoteReplacement() 进行转义文字文本,但您必须自己转义任何特殊字符。

body = body.replaceAll("(?<=\u000149=)[^\u0001]*",
                       "new text before [=12=] new text after"));