Add 0 in front of string including numbers only 1 -9

hello is any syntax in String manipulation Node,
where I need to get from Week1 to Week01
Week2 to Week02
.
.Week9 to Week09
but Week10 to still be Week10

Thank you!

Hi @Averin7777 , if it’s always the word “Week” followed by 1 or 2 digits and you always want it to be 2 digits, you could use regexReplace as follows, assuming “column1” in the example here:

regexReplace($column1$,"Week([0-9]{1})$","Week0$1")

image

This looks for strings containing Week
followed by a single digit [0-9]{1}
followed by the end of the string $
and captures the single digit (by placing [0-9]{1} in parenthesis)

and if found, replaces it with “Week0”
followed by the “captured” group “$1”

A slightly more “codey” alternative syntax, which uses regex only to determine if the pattern is found, but then uses a condition to determine the value:

string(
regexMatcher($column1$, "Week[0-9]").equals("True")
?join(substr($column1$,0,4) ,"0",substr($column1$,4,1))
:$column1$
)

which reads as
"return a string such that:
if the column1 matches the regex pattern “Week” followed by a single digit
return the first 4 letters concatenated with “0” and the 5th letter
otherwise return column1 unchanged

For simplicity it could have returned:

?join("Week0",substr($column1$,4,1))
2 Likes

Hello, thank you for response, but it does not work, the result is like this

Hi @Averin7777 , in your original post you said your strings had the word Week at the beginning eg “Week1”

If they are simply the numbers 1, 2 etc, you can use padleft

Eg

padLeft($Week$, 2, "0")

could you please write whole syntax? KNIME still showing me error

Hi @Averin7777 ,

What error are you getting?

Try retyping the double quotes as I think they may have pasted incorrectly from my mobile. Corrected now in the above post. But when I’m back at my pc I can double check.

If still not working for you can you upload sample workflow so I can check

Hi @Averin7777

A slightly different approach :slight_smile: See this wf add_0.knwf (20.0 KB)
image
gr. Hans

1 Like

I know this similar I have as well, I would like to simplify with one node String Manip. but the syntax doing only 1 as posted above. I do not know how to add this padLeft($Week$, 2, “0”)

Just to confirm, the whole syntax is

padLeft($Week$, 2, "0")

This is what it looks like:

As I said before, if it isn’t working for you, you will have to upload your workflow containing your data because we can otherwise only guess at what the problem is.

The above works for what I now believe your problem to be (which is not the same as it was in your first post)

My original solution, and the solution supplied by @HansS work for the original problem definition that you stated, i.e. that you have data such as “week1”, “week2”,… “week10”. We want to help you but if we cannot see your data, we can only go by what you tell us.

1 Like

yes it is working now, mistake was that I put wrong “0” like “0” first one is right >D

many thanks!!!

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