Extract a string from first occurrence of a separator

I have as string say ; ABCD_1234_xyz
I want to separate (or create columns) only until the first occurrence of the separator _.
So in this case I should get 2 columns one with ABCD and another with 1234_xyz

I cant use cell splitter for this since it will just separate it into 3 columns.
Is there a way to do this in KNIME?

Hi @thentangler

You can still use the cell splitter for this. Limit the array size to 2. This will capture the first group and any subsequent data in the second group. See the example below:

5 Likes

Thank you for the solution. Can this be modified to nth occurrence?
Like if I want to split it only at the second occurrence of the _ ?

Hi @thentangler

To my knowledge that would be more something for a RegexSplitter.

^((?:[^_]*_){n-1}[^_]*)_(.*) where n is the number of groups you want to maintain.

Hope this helps.

3 Likes

This is exactly what I was looking for.
can I ask one last guidance since I’m not too familiar with Regex?
n the pattern string that you provided, which characters should I modify if the delimiter is different. For example if the delimiter is now a period . ?

Have a look at the Explanation section of this regex → regex101: build, test, and debug regex

In this case, the _ is the delimiter so all occurrences of it should be replaced in the regex for the delimiter of choice. Please be aware that you have to escape certain characters in a regex.

This would be a period delimited splitter.

image

This is such a useful tool for regex noobs like me!
Thank you so much!

Hi @thentangler , here’s a quick tutorial (or cheatsheet) about Regex:
https://dl.icewarp.com/online_help/203030104.htm

It’s my goto page, and it’s helped me understand regex more and more

2 Likes

Thank you so much! This really helps!

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