Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.
Originally posted by: belliveaujdOriginally posted by: jpstory
Hi
How can I remove the leading and trailing zeros?
There's a function ltrim which only removes whitespace, wondering why there isn't one to perform similar function but to remove user specified values?
Here's a solution for you. The
no_lead_zero field uses the regExSubstitute function to replace any starting 0. You could easily substitute the "0" character with anything else you wanted to replace at the start of a string.
emit 'number'.regexSubstitute("^0+","") as no_lead_zero
NOTE: the "^0+" regular expression can be read as:
"starting from the start of the string, match the "0" character one or more times (the plus symbol)"
In your case, the no_lead_zero2 solution might be easier. This solution just requires you to convert to integer (which will remove leading 0s) and then convert back to string (optionally)
emit 'number'.int().str() as no_lead_zero2
removing leading and trailing 0s
emit 'number'.regexSubstitute("^0+","").regexSubstitute("0+$","") as no_lead_trail_0
Full code:
node:Static_Data
bretype:core::Static Data
editor:sortkey=569fe56c73676802
output:@40fe6c55598828e5/=
prop:StaticData=<<EOX
number:string
00001
00010
01000
01200
01010
EOX
editor:XY=290,130
end:Static_Data
node:Remove_Any_Value
bretype:core::Filter
editor:Label=Remove Any Value
editor:sortkey=569fe5597c087b45
input:@40fd2c74167f1ca2/=Static_Data.40fe6c55598828e5
output:@40fd2c7420761db6/=
prop:Script=<<EOX
emit 'number'
emit 'number'.regexSubstitute("^0+","") as no_lead_zero
emit 'number'.int().str() as no_lead_zero2
emit 'number'.regexSubstitute("^0+","").regexSubstitute("0+$","") as no_lead_trail_0
EOX
editor:XY=390,130
end:Remove_Any_Value