Striping part of a string

Hi,

I am trying to remove part of string with regex but it seems not working. The strings are like that:

Julia_Roberts_(actress,born_1970)
Tim_Taylor
(squash)

And I want to have only name e.g Julia_Roberts. I tried with:
_\(.*\)
which seems matching in different apps (e.g https://regex101.com/) but not with KNIME nodes. I tried with Regex Split, Substr(), String replacer, and String Manipulation but in vain.

Any suggestion ?

Best,
Mateen

Hi there @mateenraj,

how about this one?

(.*)_\(.*\)

Works both in Regex Split and String Replacer.

Or this function in String Manipulation?

substr($column1$, 0, indexOf ($column1$, "(" ) - 1)

This is under assumption second row of your example misses underscore after name :wink:

Br,
Ivan

2 Likes

Hi @ipazin,

Thanks for your quick reply, it partially solved the problem.
I missed to mentioned that there are rows which are without additional strings at the end and I want to keep them as they are (no change). Please find attached workflow.

Best,
MateenRegEx_Problem.knwf (17.2 KB)

Hi @mateenraj,

You are missing backreference ($1) in String Replacer. Add it under Replacement text option :wink:

Also this one for Regex Split should work:
(.*)(_\(.*\))?

but as you are not splitting anything I wouldn’t use it and you still have to remove additional column.

String Manipulation with substr() function won’t work obviously :smiley:

Br,
Ivan

Great, thanks :slight_smile:

1 Like

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