Inserting a new line in text by string replacer

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.

HI @jarviscampbell , what was it that you are then wanting to do at this point? Are you wanting to then aggregate these into the single cell with newlines? If so you can use a column aggregator (using regex/wildcards in the column selection) to aggregate the Data_Arr[ ] columns, and specify \n as the delimiter. You may want to uncheck the “missing” option on the aggregation to handle some columns have no data. Sorry if I’ve misunderstood what you are asking.

1 Like

Hi @takbb , at this point, I simply need to break down the double-lined rows into single lined rows. I would like to stay after that w a single column still. So when I do break them down using a colon as a delimiter, I will have only two columns and not the Array as shown above. Does that make sense?

Btw, I meant to say this before. The examples above are three different ‘iterations’, what I see on Knime, What I get as a result if I copy to a txt editor and third one your prior solution offered.

Also, I do not understand what you mean on your last post. By the time I arrive at the Array portion with Column Aggregate, columns are already separated and it’s too late w more than two columns @takbb

Hi @jarviscampbell , I was having difficult understanding what you were asking, but I was thinking you wanted to bring together your sets of Array columns, which from your last past is still what I believe you are trying to do. I don’t see what it is “too late” just because you have more than array columns, as the column aggregator works with multiple columns.

If I have data delimited by colons:
image

and I split it out into cells with a cellsplitter:

Then this is the point I think you are at.

Each of the split data columns will have a name that I can select in a Column Aggregator using Regex or Wildcards, so it doesn’t matter that there are more than two columns:

(tell it to create a list and remove the aggregation columns)

This then generates list items
image

And the lists can be ungrouped

resulting in:
image

column aggregation after splitting.knwf (11.4 KB)

1 Like

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