Replace a REGEX with a regex expression

Hi,
Aim of this post : find a node that allow to replace a text string searched with a regex expression and replace this found text using another regex expression containing regex and string combination.

So far I am using Regexreplace function in string manipulation node for a lot of string that needs to be transformed or removed.

Now, I want to replace a text searched with an regex like this : “\\nXXXX” by this other regex expression : “\\nXXXX\\n____\\n____”. As the regexreplace function in string maninipulation node admits a regex only in the search parameter but not in the replacement part, is there any other alternative I can use for this purpose ?

Thank you for any hint on this topic.

Laurent.

A few minutes after posting this I found the solution …

Regex can’t be used directly in the replacement text portion of the regexreplace function but by creating a group in the search regex pattern, this group can be referenced and used in the replacement text.
Here is an example :

regexReplace((text)$,“(\\n)XXXX” ,“$1YYYY$1____$1” )

The $1 is a reference to the group1 created by the (\\n) and thus used and correctly recognized as a new line in the replacement part.

Should you have another solution using real regex I am still happy to see it.

Laurent.

3 Likes

Hi @Laurent_Knimer , thanks for providing a potential solution to your problem. I believe though that the issue you face is possibly caused by the confusing nature of the use of regex escaping ie \\ within String Manipulation. Whilst double-escaping works in the search portion of the regexReplace, in the replace portion you need to use triple-escaping. Weird I know but go with it :wink:

Take a look at the following workflow as an example, and see if this assists you:

In the Table Creator, I create a string that I shall convert using String Manipulation so that it becomes a multi-line string. The original string is:

The Quick¬Brown Fox¬Jumps Over¬The Lazy Dog

In the first String Manipulation, I replace the “¬” character with a linefeed, using regexReplace:

regexReplace($column1$,"¬","\\\n")

so the "¬" is replaced with "\\\n", which I use the three-backslash syntax instead of two-backslash. If I had only used two backslashes, it would have replaced the original string as follows:
The QuicknBrown FoxnJumps OvernThe Lazy Dog

But with a triple escape, it correctly includes the linefeed.

This situation is further evidenced by my second String Manipulation where I then wish to add some additional linefeeds and underscores after the “Brown Fox” line:

regexReplace($first replacement$,"(\\nBrown Fox)","$1\\\n_____\\\n_____\\\n")

As you can see, it will happily accept \\n within the search portion, as you had noted, but in the replace portion, we have to use \\\n to indicate that linefeeds are to be inserted.

This does feel an odd inconsistency and is presumably to do with the levels of interpretation of the different parts of the regexReplace statement within the internals of the String Manipulation node when both interpreting the statement, and also when converting it to java. I guess it is just one of those niggling things that you need to discover and hopefully remember!

Interestingly, you can (at least in this case) opt to use three backslashes within both parts of the statement and it still works,

i.e.
regexReplace($first replacement$,"(\\\nBrown Fox)","$1\\\n_____\\\n_____\\\n")

Here is the above sample workflow that may be of interest:
Escaped linefeeds in String Manipulation regexReplace.knwf (2.2 KB)

I hope that provides some “simpler” (albeit “weird”) solutions to your problem :slight_smile:

4 Likes

Takbb, thank you a lot for such a clear answer.

I tried with 1, 2 and 4 \ and not 3. Your solution is exactly what I needed and is far better than the grouping option.

1 Like

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