Column rename regex for slightly different conditions

I want to rename the column names of a dataframe that I have using Column Rename Regex:

As an example, here are some of the column names:

Test|Weight|Day1Hour4|F|True
Test|Weight|Day1Hour12|F|True
Test|Weight|Day1Hour-0.833|F|True
Test|Weight|Day1Hour0.5|F|True
Test|Weight|Day1Hour-84|F|True

What I want to do, is to add the string _Test at the end of the time, so I would get this:

Test|Weight|Day1Hour4_Test|F|True
Test|Weight|Day1Hour12_Test|F|True
Test|Weight|Day1Hour-0.833_Test|F|True
Test|Weight|Day1Hour0.5_Test|F|True
Test|Weight|Day1Hour-84_Test|F|True

The first thing I tried was:

Capture

But of course, this is not working properly:

Test|Weight|Day1Hour4_Test|F|True
Test|Weight|Day1Hour12_Test|F|True
Test|Weight|Day1Hour-0._Test833|F|True
Test|Weight|Day1Hour0._Test5|F|True
Test|Weight|Day1Hour-0.84|F|True

I though of encapsulating the last part of the string, using this:

Capture1

But for some reason it adds the string _Test at the end, like in:

Test|Weight|Day1Hour4|F|True_Test

Of course I could rowsplit the data and execute two different column rename regex and then join, but I have a LOT of columnsto rename, and this would take a lot of time.

So I would love to know if there’s a way of renaming them all at the same time, with just one regex command (or other option).

Hello @RoyBatty296
What about a work around with ‘Extract Column Header’ node? then unpivot the headers to have a single column. Once you have your headers in a column you can use a simple String Manipulation rules.

Renaming the columns afterwards with ‘Insert Column Header’ node

BR

Thanks @gonhaddock, that sounds like a good idea.
But how can I use string manipulation node to achieve my purpose in this particular case?

Oh… Wait!
I have to do changes

The simple one:

image

String Manipulation:
replace($ColumnValues$, "|F|True", "_Test||F|True")

20220127_conditional_column_rename3.knwf (35.5 KB)

BR

1 Like

@gonhaddock already provided the solution, kudos to him.
Just as an addon for future reference. A regex like this should work

2 Likes

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