manipulation: Delete the digits on the left if they are zero

Hello KNIMER,
grafik

As you can See in the picture, I have a column with the name “Serien”, if a number starts with zero I want to delete it automatically. If there are more zeros it shall delete all of them.

Thank you very much for your help :slight_smile:

Hi @Alex1990,

typical Regex topic :wink:
Use the String Manipulation String with

regexReplace($Serien$, “^0+”, “”)

regards, Tommy

3 Likes

Or the String Replacer:

image

But I am wondering: is there any way to do this without using regexes? :thinking: For many beginners regexes are a big scary mystery. I could come up with some crazy complex loop constructs, but there has to be a simpler way… Something like the “stripstart” function in the String Manipulation node, but that one only does whitespace.

Best
Aswin

2 Likes

Thank you @tommy it works!!

@aswin as long as the regex works for me I am happy :smiley:

But I appreciate your help :slight_smile:

2 Likes

To answer my own question, one could do this in a String Manipulation node…

replaceChars(stripStart(replaceChars($Serien$, "0", " ")), " ", "0")

not very elegant though, and only works if you don’t have whitespace…

Edit: maybe using a tab instead of a space in the expression above would work better, since tabs are less likely to occur in normal strings.

2 Likes

Hi Aswin, I also hear a common transformation problem.
Using (several) loops wouldn’t be smarter.
As this issue should be in the TOP10 of the Regex world and the pattern is quite simple, I think providing the Regex-solution doesn’t overwhelm sb.
However, you’re right, a function like “stripstart” would be great with an option to delete the first char or the first n chars.

regards, Tommy

1 Like

Hi all,

you can find this function in the

column expression

node.

Cheers, Iris

Actually, the String Manipulation node has also the Strip Function. :slight_smile:

1 Like

But can you make those strip functions strip anything else than whitespace? :slightly_smiling_face:

1 Like

Hi,

I think it is meant only for whitespace.

@Iris: correct me if i am wrong.

Regards,
Pavan.

1 Like

Hi there all,

@pawanmtm correct, only for whitespaces.

@Aswin Java and JavaScript (String Manipulation and Column Expressions node) native strip/trim functions do not receive any argument and only can remove white spaces. I guess enhancement of this functions would mean to use regex :slight_smile: Python strip function for example accepts argument and can do what you are looking for.

@Alex1990 seems to me that you can use String To Number node to remove zeroes :wink:

Br,
Ivan

2 Likes

But in @Alex1990’s example the “Serien” column also contains letters sometimes…

1 Like

You are right. Didn’t see that X although it is first char in first row :smiley:

Br,
Ivan

1 Like

I would do something like

join((replace(substr($Serien$,0 ,1 ),“0” ,"" )),substr($Serien$,1))

I normally prefer to create new column for each string manipulation to have more control on the final outcome and then collect in a more complex string manipulation

image

1 Like

Cool alternative! But it looks like that this method works with “0987” but not with “0098”. @Alex1990 wrote “If there are more zeros it shall delete all of them”. Can it be changed to replace multiple 0s? One could put it in a recursive loop and run it multiple times :slightly_smiling_face:

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