Text Replace and Keep digits (with no decimal points)

Hi, I’m strggling with something I think should be straightforward.

I have a variable of ‘X’, which could be comprised of any text or digit combinations.

I want to remove all text and if there are numbers in the string, to just leave these behind.

e.g. if

“X” = “100+ Eggs are available”

I want the output to be “100”

The code I am currently using in a SetVariable module is as follows:

{{if(length(replace(5.parking; “[^0-9]”; emptystring; “g”)) > 0; parseNumber(replace(5.parking; “[^0-9]”; emptystring; “g”)); 0)}}


But it throws an error if there are no numbers in the string, because it can;t then ‘parseNumber()’.

How do I remove all text characters and just be left with a number (with no decimal points)?

Thanks

Hello @Ed1 and welcome back to the Make Community!

I think you might need to specify more conditions/rules here.
What if you need all the numbers from something like “100+ Eggs are available at 2:00pm”? Then your result would be 100200 and that’s probably not ideal.

Maybe only get all the numbers from the first 5 characters of the input or something like that?
Are you guaranteed to always have the number at the beginning of the string?
The more examples you can provide, the better, but you could probably get away with a Text Parser Match pattern module for this.
You may try using ChatGPT to give a regular expression to get what you’re looking for and the more example data you can provide, the better the result (in most cases!)

You could try something like this (remove everything from the first non-digit onward):
image

Thanks very much Donald.

To be honest the patter of the letters and numbers will only ever either be leading with numbers and be follower by a small amount of text, or entirely consist of text.

I’m testing your solution right now and will feedback is it’s sufficient.

I’m not great with REGEX yet, so a little lots with it.

Thanks for the feedback though!
Ed