Filter numbers (0-9) in a variable and count them

I can get the count part, but can’t seem to filter the numbers (0-9) (single digits, could also be in a row) of a variable.
Variable ='s
"this is the day 12345 that the 6789"
would return 123456789 to the destination variable and then would count the number of digits which would equal 9.

You could use a regex:

\D: Match any character that is not a decimal digit.


Count Digits.kmmacros (1.9 KB)

1 Like

woof, ok, - thank you, soooo , when the variable gets filtered, you don’t send that to another variable, it REPLACES, the filtered variable with the result. ok, seems bit weird to me, I’d like to keep the original variable intact for other reasons, but no worries, I’ll just keep it in another variable. - Many thanx Tom.

I agree, the "Filter Variable" options which return a count to the same variable are weird, and counter-intuitive to me. But you can use the CHARACTERS() function instead, in a Set Variable to Calculation Action:

Ah, very nice indeed. Thank you ‘J’, it will take me some time to even start to scratch the surface here. Going to back up a bit in the next week or so and read the wiki on fields, tokens, variables and calculations. Thank you so much for your time and help along this path. Much enjoyed. -

This is true for both actions here. Neither of them sends the result to another variable. That is, both actions in the macro work as “destructive” filter (although the first one is not named “filter”):

  • Original variable:
  • Contains: this is the day 12345 that the 6789
  • 1st filter: Search and Replace
  • Eliminates all non-digits. After that your variable contains this: 123456789
  • 2nd filter: Filter with Character Count
  • Self-explaining. Your variable now contains this: 9

So, if you want to preserve the original value of the variable, you have to make a duplicate of the variable before the first filter (the Search and Replace action). Replacing the 2nd filter with a Set Variable to Calculation action won’t help you here.

Understood, Thank you Tom

Actually, I think it does, @Tom:

By replacing your Action:

with this one:

I have preserved the value of wTmp, and stored the count in a new variable.

Yes, but he wanted to preserve the original value. (= before the Search & Replace). Your change preserves the already altered value (123456789)