RegexReplace with capture groups

I’m trying to replace a json element in a large block returned from an API call. The JSON structure is variable between calls but I know the exact element I am trying to replace. From what I can tell the easiest way to do this is with the String Manipulation node RegexReplace.

Given a JSON body that looks like this (most of the body omitted for clarity):

, {
“name” : “part number”,
“value” : “99”
}, {
“name” : “quantity”,
“value” : "9.0 "
}, {
“name” : “quantityUnit”,
“value” : “each”

I’m using the RegexReplace function but it does not seem to work. In the below example I’m attempting to replace the capture group $1 by “35.3”.

regexReplace($body$, "(\"name\"\s*\:\s*\"quantity\",\s*\"value\"\s*\:\s)\"(.*)\",", join("$1","\"35.3\","))

Unfortunately this replace does not seem to work. The result is that I return the exact same JSON that was in $body$

If I use the same regular expression in the Palladian Regex Extractor node it works as expected and I can extract the quantity value of “9.0” from the $body$

Am I using the RegexReplace function correctly? Is there some trick to accessing the capture groups?

1 Like

Hi @weatherh,

This will be the way Java needs REGEX formatting, I always find it best to get your match / group working in, the select Code Generator and choose Java, this will give you the correct pattern.

For example, \s would become \\s

A literal \ becomes \\\\ I think.

Have a go at regex101, if no-one else has come back to you before Monday, I’ll take a look then.




Hi @weatherh

Did you already check the Regex Extractor?
gr. Hans


Yes. Regex extractor works great with this regex but unfortunately this doesn’t help with the replace function.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.