Search code examples
regexknime

How can you remove specific instances of a character, but not all?


I have a list of names that are separated by a semi-colon:

BorisovaSvetlana A.;KimHak Joong;PuXiaotao;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;LiuHung-wen*;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?;?

I would like to remove all of the "?;" but only if they occur in that pattern.

I am using KNIME and tried to use regexReplace($col1$,"[?;]","") or regexReplace($col1$,"?;","") in the String Manipulation node, but all instances of ? and ; are removed.

I want the output to be

BorisovaSvetlana A.;KimHak Joong;PuXiaotao;LiuHung-wen*

but the actual output is

BorisovaSvetlana A.KimHak JoongPuXiaotaoLiuHung-wen*

Any guidance in the right direction would be greatly appreciated, as I am new to both KNIME and regex.


Solution

  • You can escape the ? instead of putting it to the alternatives like this: \?. As the \ needs to be escaped because of the quotes, the proper escaping is \\? within the String. So the following should work for your request:

    regexReplace($col1$,"\\?;","")
    

    For your input it produces:

    BorisovaSvetlana A.;KimHak Joong;PuXiaotao;LiuHung-wen*;?
    

    Workflow showing the result and the regex