I am searching for a regex in which I can look for certain tags in FIX and then add info to them. I thought to refer to the tags as a string and search it in a regex using the ReplaceAll method. However, I don't know how do it when I don't want to replace the string in the regex but the string after it.(meaning the content in the tag).
Since the 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 pattern.
Example: To replace value of tag 49
body = body.replaceAll("(?<=\u000149=)[^\u0001]*",
Matcher.quoteReplacement("new value"));
Explanation
(?<= 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
If you need to add text before/after/around the existing test, just insert the matched text, using the $0
reference, which also means you shouldn't use quoteReplacement()
to escape literal text, but you'd have to escape any special characters yourself.
body = body.replaceAll("(?<=\u000149=)[^\u0001]*",
"new text before $0 new text after"));