Regex replacer $

I’m trying to clean up some combined columns using String Manipulation or String Replacer but the Regex is not working as expected.

Here is my data:
London, UK
London,
,

And the results I want:
London, UK
London
[empty string]

I would like to remove the ", " when it is at the end of the string.

The regex I am using:
regexReplace($Address Line 2$, “,\s$”, “”)

,\s$ works in online Regex websites like regex101 but in KNIME it isn’t changing the string at all.
I have also tried using the regex in String Replacer with the same results.

Can anyone help?

Hi @plattrk

In KNIME you need to escape special characters, like the slash.

Regex101 can help you translate your code for this purpose. In the left hand menu under tools, click on Code Generator. Select Java and look for the contents of the final String regex =. If you paste that in the RegexReplace, then it should work.

You will see that ",\s$" becomes ",\\s$"

Hope this helps!

2 Likes

Hi, I’m still not seeing a difference :frowning:

Expression in String Manipulation:
image

Output:
image

Then your data input is probably slightly different. [,]$ should also work if you don’t have an empty space behind it.

4 Likes

Hello @plattrk
Test this String Manipulation’ node:

regexReplace($text$, "(.*)[,]$", "$1")

or

regexReplace(strip($text$), "(.*)[,]$", "$1")

BR

3 Likes

Thank you both.
I managed to get it working.
For some reason Column Combiner was stripping the space in the delimiter, so my original Regex was not correct.

1 Like

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