String Manipulation - Conditionally replacing first two characters only

Hi all,

I’ll preface this by saying I’m not sure if String Manipulation can do this, or if it’s a Rule Engine problem.

I have a string column $Time$ that’s in a “hh:mm:ss” format. There are instances of hh being 24, 25, or 26, which need to become 00, 01, and 02 respectively

I’m learning the rule engine/string manipulation nodes (and java generally), but have not yet figured out if/else syntax.

Basically I want to replace the column with something along the lines of:

if substr($Trip$,0,2) = 24 => join(“00”,substr($Trip$,2))
elif substr($Trip$,0,2) = 25 => join(“01”,substr($Trip$,2))
elif substr($Trip$,0,2) = 26 => join(“02”,substr($Trip$,2))
else => $Schedtrip$

Could anyone advise on the syntax for that logic? Thanks!

Hi there @braunnz,

String Manipulation node can’t do it as it doesn’t have multiple if logic (not sure there is other way like regex for example) and Rule Engine can’t do it as it doesn’t have join (and substr in a result) logic. But Column Expressions got both!

Check this topic for more info on Column Expressions or search forum for any other as there are plenty of them.

Try it out and if you have any problems feel free to ask :wink:

Br,
Ivan

3 Likes

Hi @braunnz,

In addition to the solution by @ipazin, you can also use this expression in the String Manipulation node:

regexReplace(
	regexReplace(
		regexReplace($Time$, "^24(.*)", "00$1")
	, "^25(.*)", "01$1")
, "^26(.*)", "02$1")

I prefer if-else statements in the Column Expressions node suggested by @ipazin, but decided to provide this solution since Ivan mentioned regex.

:blush:

5 Likes

Totally new to me, but looks very powerful – thanks for the resource!

That worked perfectly, thank you!

1 Like

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