help with gsub (finding numbers)

Hi,

i have strings like

“i spend $350.20 for my car”

“20$ for my dog”

“i used 12.30Euro for my toys”

“simple 8.21 spent”

i like a function that simple filters that numbers

350.20

20

12.30

8.21

please can u help

thanks

chris

Hi Chris,

Took a look at string manipulation and came up with the following code that work on your examples.

[lua]

local function getNumber(value) local result -- check for number with decimals result=string.match(value,"%d+.%d+") -- check if nil if not result then -- check for number without decimals result=string.match(value,"%d+") end return result end local myString="i spend $350.20 for my car" print(getNumber(myString)) --\> 350.20 local myString="20$ for my dog" print(getNumber(myString)) --\> 20 local myString="i used 12.30Euro for my toys" print(getNumber(myString)) --\> 12.30 local myString="simple 8.21 spent" print(getNumber(myString)) --\> 8.21

[/lua]

There might be a more refined string pattern for this but its too early in the morning for that.

Hope it helps mate  :slight_smile:

I would use a set of characters [%d%.%+%-%,]+ or something like that to make sure you pick up other valid numeric punctuation  (untested).

Rob

Hi Chris,

Took a look at string manipulation and came up with the following code that work on your examples.

[lua]

local function getNumber(value) local result -- check for number with decimals result=string.match(value,"%d+.%d+") -- check if nil if not result then -- check for number without decimals result=string.match(value,"%d+") end return result end local myString="i spend $350.20 for my car" print(getNumber(myString)) --\> 350.20 local myString="20$ for my dog" print(getNumber(myString)) --\> 20 local myString="i used 12.30Euro for my toys" print(getNumber(myString)) --\> 12.30 local myString="simple 8.21 spent" print(getNumber(myString)) --\> 8.21

[/lua]

There might be a more refined string pattern for this but its too early in the morning for that.

Hope it helps mate  :slight_smile:

I would use a set of characters [%d%.%+%-%,]+ or something like that to make sure you pick up other valid numeric punctuation  (untested).

Rob