How to split an attribute with integer to the first 2 digits
I have an Attribute with integer values (4 Digits) and have to extract the first two Digits and write them to a new Attribute.
In RegEx I think it is ^\\d\\d. How can I do that in FME?
I tried with ExpressionEvaluator and AttributeSplitter @FindRegEx(@Value(Attributename)^\\d\\d)
but it does not working.
Thanks for helping.
Page 1 / 1
Hi @mewi, there should be some ways, using this math expression with ExpressionEvaluator is also one of them. Assuming that an attribute called "attr" always stores 4 digits integer value.
@int(@Value(attr)/100)
Ou, I didn't see this solution. That's quite easy. Luckily I Need the first 2 Digits and not the last ones .Thank you @Takashi!
FME is not explicit in its data types (i.e. whether it's a number, string or datetime attribute), so you can treat any integer as a string and extract its first two digits by using the SubstringExtractor:
Note: @takashi and my solution give the same result if your input value is 4 digits exactly, but they give different results if it's got less than or more than 4 digits!!
In addition if you wanted to use the regex you could use a stringsearcher with your regex
Thank you for the additional answers @egomm, @arnold_bijlsma. This is very helpful!
If i don’t know the length of Integer Value, then how do i extract the integer value from a Attribute.
I generally use StringReplacer + RegEx Groups that allow you to bracket the part of the text that wish to keep as the output.
As a more generalised version of the above solution. Can find the first 2 digits of any length string like this.
^(\d{2}).*$
Another variation is if want to find the first 1-3 digits say, for any width string is
^(\d{1,3}).*$
In RegEx, you can “call back” return a Group string found with notation like \1 for the first group, \2 for the second group etc. In FME then for Transformers like StringReplacer in RegEx mode, then the replacement string can be set as “\1” which will tell the Transformer to return only the characters in the 1st group.