Remove numbers after last underscore, in case there's numbers

Hi,

I need to remove numbers at the end of a string after the last underscore. However, the text before the last underscore can also contain numbers, which need to remain. So a simple replace all numbers won’t work. In the list of values below I only want to remove the numbers at the end.

As you can see the amount of characters before the numbers differs per line. Also the amount of numbers at the end can differ (from 1 - 4 numbers). And some lines don’t have numbers at the end. And some lines have a different amount of underscores.

I think this has to be done using regex, but I’m not knowledgeable enough on this :frowning:

The first line should look like this:
AEV_Z_EXRP_O2C_O

Example column
AEV_Z_EXRP_O2C_O_941
AEV_Z_OSLC_O2C_O_563
AEV_Z_EXRP_O2C_O
ATV_S_LOC_O2C_O_656
ATV_S_LOC_O2C_O
BEV_Z_ICRP_O2C_O_215
BE_V_ZR_ICRP_O2C_O
BEV_Z_LRC_O2C_O_1406
CHV_S_LOC_O2C_O_669
CHV_S_LOC_O2C_O
NOBONDTERM_O2C_O

Thanks!!
Rob

Hi @robvp ,

You need the regex for it.

Example:

With string manipulation node, use regex to find and replace.

Expression:

image

regexReplace($values$,“(.*)_\d+$”,“$1”)

(.*) for anything
_ as separator
\d+$ to find the last number from the string
$1 will set the string before the separator _

Thats it,

Result:

image

KNIME_project_regex.knwf (12.3 KB)

Thanks,

Denis

4 Likes

Hi @robvp

You don’t need regex. You can accomplish this with a String Manipulation (reverse function) and a Cell Splitter node. reverse_split.knwf (21.0 KB)
image
gr. Hans

3 Likes

Use chatGPT. It can write regex very well.

1 Like

I made with just one node that you need to use 3. For performance, it’s better way to use less memory and process with less steps. Can be more complex for someone, but have benefits at all.

it’s nice to see some examples here to build a huge repository of ideas.

Thanks,

Denis

1 Like

This didn’t work. For example value AEV_Z_EXRP_O2C_O was missing the O at the end.

1 Like

Maybe at the file, this “O” was a zero… \d works only for digital numbers, not letters…, think about it…

Tks,

Denis

Apoologies, I was referring to @HansS suggestion.
@denisfi worked great :slight_smile:

If this challenge is close, just mark as solved ok?

Tks,

Denis

Already did that a few days ago :slight_smile:

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