Create a string manipluation that replaces the 4th the last space with the expression xxx

Hi Everyone.
I tried to create a string manipulation for knime that replace the 4th last space of my string with the expression “xxx” however it didn’t work.
Do you have a solution for this or an idea what I did wrong?

regexReplace($column1$, “(.?\s)(?:(?:.?\s){3})(.*)”, “$1 x xx $2”)

many thanks in advance
Miriam

Hi @miriamgurk , welcome to the KNIME forum.

A couple of quick tips: when you repost you regex expression, it is best to highlight it and press the </> (preformatted text) button on the toolbar
image

That way the forum software won’t try to interpret/modify the expression and we’ll be able to see and test exactly what you have written.

Also it is useful to share sample data and expected output as well as writing the description, to ensure our interpretation of what is being asked is correct.

You could try this expression:

regexReplace($column1$,"(.*)[ ]{1}((?:[^ ]*[ ]{1}){3})","$1xxx$2")

image

2 Likes

Hello @miriamgurk and welcome to the KNIME community.
It isn’t fully clear to me your challenge. From description I can assume that your text string has always four spaces, aiming to replace the fourth of them with xxx

You can test with the following code:

regexReplace($text$, "(.*)\\s(.*)", "$1***$2")

Otherwise, can you provide a text sample and expected output?

BR

2 Likes

As per @gonhaddock’s comment, after posting I realised that I wasn’t quite sure what is supposed to happen if you have several spaces in succession e.g.

This string     contains      many       consecutive          spaces

and I also handled only space, and not other “whitespace”, so again you may need to be more specific for these “edge” cases.

3 Likes

Thanks a lot everyone! This was exactly what I was looking for! :slight_smile:

1 Like

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