how to add more than one expression in string manipulation?

Hello,

i am wondering how to add more than one expression in string manipulation? for example in this Node expression i wrote:

replace($colnameA$, "abcde","test1","w");
replace($colnameA$, "fghhi", "test2", "w")

and execute, then i gote warning "invalid settings: unable to compile expression"

does anyone can help me to fix this problem? Thanks a lot!

Kai

You need to nest the expression, e.g.

replace(replace($colnameA$, "fghhi", "test2", "w"), "abcde","test1","w");
 

2 Likes

ok, but it will looks very chaos if i want to replace more than 10 text. Is there no other solution?

Do you want to replace the complete contents of a cell with another string or really parts of the string? If the former is true the Dictionary Replacer may be  more helpful than the String Replacer.

Hi Thor, hi Kay,

this is exaclty my problem, I'd like to replace some 10-15 different expressions within a cell (not substitute the whol cell e.g. with the cell replacer node).

In my case I do have a column with multi-word expressions and would like to do the above transformation (to get rid of GmbH/ AG/ & Co./ & Co. KG [...] and the manifold misspelled ways of those ;).

Is there a neat way to do this?

Hi all,

I'm struggling with exact the same problem than Tim:

I'd like to replace a great variety (30-40) of different words and signs within a text column and avoid a chaotic nesting of 30-40 IF..THENs

Please: is there any node to achieve this (if I understood it well the StringReplace Dictionary won't do it)

Best regards

Bernd

It's an easy task using a recursive loop. Please see the workflow attached to this post.

Best,
Marc

3 Likes

.. thanks very much Marc.

I took the workflow and adapted it to our customer's.
But executing it caused the following errors at the node "String Manipulation"

How can I fix this?

Bernd

Hey Bernd,

I have no idea, I haven't seen this before. Looks like a problem deep within KNIME.

Sorry!

Best,
Marc

attached is a workflow that recursively substitutes.

1. takes the search, replace dictionary from a table

2. groups the searches which maps to the same replacement into a regular expression to reduce the number of iterations

3. 2 port recursive loop to pass both the dictionary and the data through 

4. string replace using regular expression and all occurences

5. number of iterations determined by the number of distinct replacements

 

Thanks David

Thanks Marc

Hey Kai,

to apply a multiple string replacement you can use the R-Snippet node.

Here is an example of the R-Code:

install.packages(“stringr”)
library(“stringr”)
knime.out <- knime.in
knime.out$“Column1” <- stringr::str_replace_all(knime.in$“Column1”, “pattern to replace_1”, “replacement_1”)
knime.out$“Column1” <- stringr::str_replace_all(knime.in$“Column1”, “pattern to replace_2”, “replacement_2”)

…and so on.

If you like to append the entire string that contains the replaced string as a new column:
knime.out$“Column2” <- stringr::str_replace_all(knime.in$“Column1”, “pattern to replace”, “replacement”)

I think, for a manageable amount of string replacements this could be a feasible solution. In case of large amounts it would be smarter to build up an dictionary and apply it by looping the dataset. Unfortunately you can’t use the KNIME node “String Replace (Dictionary)” because it does’nt support the replacement of substrings, so you have to use a workaround like the R-Snippet.

An additional experience:
If you want to replace substrings that contain patterns of multiple dots like “banana…” for instance, you have to be aware, that stringr::str_replace_all uses RegEx. A dot in RegEx representing a wildcard. To escape the widcard funktion of multiple dots just add square brackets like this:

knime.out$“Column1” <- stringr::str_replace_all(knime.in$“Column1”, “[.][.]”, “.”)

1 Like

I personally prefer the Java Snippet (simple) node for this task.
It’s the most intuitive implementation I have seen so far.
The code used in the Java node is easier to understand than in the R node, AND you don’t need to install an addon (and a local R installation) in order to use it.

The node content looks a little bit like this:
image

Be careful with characters that need to be escaped with a \ for the replace() function to work.
There is a list of all such characters somewhere in the middle of the following page:
Escaping characters in Java | CodeGym

4 Likes

Works well! Without a doubt the simplest solution for multiple replacements