How to replace everything from string with PCRE / REGEX result

It could happen you have a string and would like to replace everything except what you select with the PCRE / REGEX.

For example we have the string:

And would like to remove everything except “Roundball-1252789.1.png”.


Your first thought might be to use the “String: PCRE replace” mapper with the following regex to select just the content behind the last forward slash: “([^/]+$)”.
This will however not work with the “String: PCRE replace” mapper as that content is not what we are trying to replace.

To fix this you need to invert the regex, in this case: “(^(.*[\/]))” to select everything up until the last forward slash.
And then use the “String: PCRE replace” mapper to replace it with an empty value (null).
This can be done by replacing it with “$2”, because with this regular expression $2 will always be null.

The input:


The output:

  "path": "Roundball-1252789.1.png"

This specific use case can however be solved much easier!

The answer is in the topic

Could you explain why $2 refers to null specifically? If we lets say compare it to $1 for example?

The $1 container will be filled with the first result of the Regex, thus “/ProductImages/image-thumb__14__api_product_image/”.

This specific regex will only have one hit, that’s why the $2 container will always be null.

1 Like