Inserting a new line in text by string replacer

Hi,

I am not so familiar with REGEX and would appreciate a little help. In a string cell with text like

1. text. 2. othertext. 3. justanothertext...and so on

I want to insert a newline/carriage return just before the numbers with the StringReplacer to get the following result:

1. text.

2. othertext.

3. justanothertext...and so on

I use ( )([0-9]{1,3}\. ) for the text to replace which seems to work and \n$2 for the replacement text. It inserts the numbers again but not the new line. Instead I get a 'n1.' for example.

Any ideas? Thanks in advance, Jerry

 

 

 

How about \r\n$2

simon.

Thanks Simon,

I get rn1. as replaced text. The String Replacer does not seem to recognize special characters like \r or \n and when I choose 'Regular expression' the 'use backslash as escape character' is dimmed. 

Best regards

Jerry

Hmm, I've tried absolutely everything to get this to work, but cant.

Sounds like a bug, or RegEx isnt implemented in the replace string box?

Simon.

Thanks for trying

Jerry

A bit tricky, but I hope the attached workflow gives a workaround for your problem.

Cheers, gabor

The replacement text doesn't recognize special escape sequences (only backreferences). Therefore you cannot insert newlines or tabs, for example.

Thanks a lot, Gabor

The workaround works excellent.

Kind regards, Jerry

 

 

Was wondering if anyone has gotten this to work with tabs?

Using aborgs workflow, and changing \n to \t doesn't seem to be working.

 

Also wondering if this was ever solved. I have caught myself needing this several times in the past and always have to find a different solution. I seem to stumble across this multiple times here and there. Any help is appreciated. Thanks in advance.

J.

Use a Column Expression with regexReplace(column("Column"),"\t","\n")

image

This topic is 9 years old and a lot of KNIME versions ago so it’s better to create a new topic next time time you come across such an old one :wink:

3 Likes

I understand that, unfortunately regexReplace doesn’t work as shown.
addNewLine.knwf (6.3 KB)

1 Like

Because the input text that you use in that script not contain tabs but a single whitespace :wink: (#1 is mine, #2 is yours)

image

2 Likes

I see. I tried to replicate yours but the output wasn’t the same. That’s okay. The output file I get only contains spaces. I tried “\s” and “\s”+ with no luck. I have also tried for example: regexReplace(column(“Column”),“(IPsec)+(:\s)+(.+)”,“\n”) for the following use case:

input
abcdefgh256 IPsec: (2)AES256

output
abcdefgh256
IPsec: (2)AES256

I am finding amazing that a single CR on an editor it’s seeming to be very hard on such powerful tool such as Knime. I really hope I am wrong and there’s a node out there to simply add a CR based on a specific word it finds along the text sending it to the new line, as well as a ‘white space’ trim for beginning of lines. I have been trying ‘Column Expressions’ with ‘strip(" ") or double space’ and ‘stripStart(" ")’ without any luck.

Also, unfortunately \n is not acting as expected for me no matter what I try. I have tried to even simply match the expression. “IPsec” and then skip the line w no luck. The regex matched on regex101.com but didn’t do the expected on the function.

leading characters example was found here and solved my problem: Removing leading characters - #2 by ipazin

as far the regex goes:
@ArjenEX The regex I mentioned does not match, this is what I used:

I would opt for a Regex positive lookahead in this case.

regexReplace(column("Column"),"[\\s](?=(IPsec:))","\n")

Output:
image

Reference: regex101: build, test, and debug regex

Make sure you take along the Java compatible version from regex101 to KNIME by using the Code Generator, Java and the contents of the final String regex

Next to strip, can also recommended the removeDuplicates() function. I often nest them as well for the most optimal result. For example,

strip(removeDuplicates(regexReplace(column("Column"),"[\\s](?=(IPsec:))","\n")))

Hope this helps in some way!

4 Likes

@ArjenEX absolutely it did. I knew there was some sort of different regex just had no idea which one would be. That will definitely help me. Thank you! Last question about this.

After I added a new line. By opening this on excel and selecting the wrap cell function, I can see the second line on the cell. So far so good. Coping the entire column to a Notepad, I get quotes from the beginning of the line to the end, like this:

“Encryption : IKEv2: (1)AES256
IPsec: (2)AES256”

I do not see these quotes on Knime. I suppose there may be a way to remove them.

2nd to last. I have several words that will need to be added to the same exact expression of ‘Column Expression’, anyway I can use this with variables and/or some sort of dictionary?

Last but not least, how can split this double-line row into two single line rows?

Thanks again.
J.

Hi @jarviscampbell , here is another possible way to do similar manipulation without using regex. I don’t know if it is suitable in your case, but possibly useful to see other ways too. The last part may assist with you question re splitting a cell into a new row.


Initial data is
image
but although you cannot see it, there is a tab separating the letters and numbers
ie

abc[tab]123
def[tab]456

So at the end of “part1”, we have this:

image

and at the end of “part2” we have this:
image

Cell and row splitting with tabs and new-lines.knwf (15.3 KB)

3 Likes

Thank you so much @takbb , I will see if your option works for me tomorrow w a different problem. I believe regex might do it at this point for this one. I appreciate the help.

J.

Hi @takbb I tried your solution actually, w my current data. Unfortunately using colon instead of a tab to split (which is what would work for me based on what I have, gave me strange output)
I’ve started from the point that I already had some sort of ‘column aggregated’ since I have eg: < Connection:8.8.8.8 > data already. Ideas?
image

Br,
J.