Get numbers from a string

Hi team.

I need to do a sum of individual numbers from a string. One point of observation is that the string have different lengths. For instance:

e.g.:1) variable = “123456789” I need to sum 1+2+3+4+5+6+7+8+9
e.g.: 2) variable = “1000232323213” I need to sum 1+0+0+0+2+3+2+3+2+3+2+1+3

** As variable field is a result of a formule the lengths can be differents.

Rgds
Eduardo

Welcome to the forum @eduardoam.

My suggestion is to use the RegEx Extractor node to extract the digits. After that, some basic manipulation gives you the sum:

image
image

4 Likes

Hi @elsamuel,

Thank you very much for your quick answer!!

I’ve tried to download the workflow, but it is showing a message error:

First show me a message regarding to “missing node extension”

Second one, when I click on Yes option to install, it is showing a message that there are “no suitable extension found”

Do you know what I nedd to do here?

Regards
Eduardo

Hi @eduardoam

The RegEx extractor is not a “standard” KNIME node. You need to install the extension Palladian for KNIME from the an update site. See the link. And add the link to your KNIME - File - Preferences - Install/Update / Available Software sites.
Schermafdruk van 2023-05-25 16-02-02
gr. Hans

2 Likes

Hi guys,

That’s right, Regex Extractor works very fine with it…

I created a table with some examples as string column. If you have other type, will works too.

Then I set Regex as “\d” (a digit) and the output as rows to split it into rows.

After that, just use the groupby node to set the first example number as flag and the output to sum all rows.

Result:

image

Also, you could do it with some steps more (create variables, make some calcs for each value, create a chunk loop to validate row-by-row and make some calc to sum each value to a total variable… more work to do, but you’ll have the same result.

Something like this:

Looking row by row with chunk loop, then, split number by number using substring function and variables to change the position and save as a row result until the loop end and then, using a groupby node to sum all results… more complex, but it works.

Example here:

KNIME_project_string_count.knwf (92.3 KB)

BR,

Denis

Hi @HansS,

Thank you very much for your help, now it is working.

Rgds
Eduardo

1 Like

Hi @denisfi,

Thank you very much for your help.

Your workflow is exactly what I need, using a dynamic length.

Rgds
Eduardo

I’m happy to know about it… good luck!

BR,

Denis

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