fogx
fogx

Reputation: 1810

Lua string.match captures in a single variable?

i want to remove separation characters from inbetween numbers in lua. For example: 19,300 -> 19300.

I am able to extract the two "parts" of the number using

a,b = string.match(amount, '(%d+),*(%d*)')

but have to store the results in two separate variables first. I would like to have a clean way of storing all capture groups as a concatinated, single variable, instead of having to do c=a..b in an extra step. Is there a way to achieve this?

Upvotes: 1

Views: 917

Answers (2)

Leslie Krause
Leslie Krause

Reputation: 467

Yes the captures can be put into an array thusly:

local res = { string.match(text, pattern) }

The array res will contain the captures, and #res > 0 can be used to determine if the match succeeded.

Upvotes: 1

Alexander Mashin
Alexander Mashin

Reputation: 4617

You can simply remove all non-digits: local stripped = string.gsub( amount, '%D', '' ).

Upvotes: 2

Related Questions