0
votes

I have a string and want the last digit occurrence (for now my string.match only take the first occurrence)

testvar = "350.11$ i paid, 320$, with visa, cash, 200.21, mastercard, shopping, food"

print ("last number: ".. string.match (testvar, "(%d*%.?%d+)"))

in that case I want 200.21 as result (and not 350.11 as by now)

2
string.match (testvar:reverse(), "(%d+%.?%d*)"):reverse() - Egor Skriptunoff
string.match (testvar, ".*%f[%d.](%d*%.?%d+)") - Egor Skriptunoff
@Egor, those are both good solutions, why not post them? - Wiktor Stribiżew
@EgorSkriptunoff +1 from me for the reverse() trick. However, it should be noted that it will work only when the pattern happens to be symmetric (a palindrome if you will). The given pattern, for example, while it will match the number .100 when going forward it will fail to match correctly with reverse() - tonypdmtr
@tonypdmtr - The pattern was also reversed to work correctly. - Egor Skriptunoff

2 Answers

1
votes

Try this

local last
for w in  string.gmatch (testvar, "(%d*%.?%d+)") do
    last = w
end
print(last)
0
votes

Thanks to Egor and all u other guys, u are amazing.

i choosed this two as answer

string.match (testvar:reverse(), "(%d+%.?%d*)"):reverse()
string.match (testvar, ".*%f[%d.](%d*%.?%d+)")

what do u think is even the better one?