A regexp question, how to capture 123,456 ?

Posted by Fred on Tue 03 Jan 2006 06:58 AM — 4 posts, 16,038 views.

#0
i'm trying to capture this kind of information
ABC 123,456 (100%) or ABC 1,234,567 (100%), ABC 12 (100%)

My trigger is
ABC(.*)\(.*

here is the problem, 1% = 123 456, I have a missing "," sign here.
when i try to assign this value to a variable, it will equal to 123.
the value which i'm looking for is either "123,456" or "123456"

btw, string.len(%1) = 3
print(%1) = 123 456
Australia Forum Administrator #1
Your problem here is partly that you haven't quoted %1.

This is being fed to Lua as:


print(123, 456)


Thus, it is printing two numbers.

For a start, you need to quote it, like this:


print("%1")  --> 123,456


However now your problem is you have 2 numbers inside a string. A simple "gsub" to replace commas by nothing will convert it to a single number (inside a string):


num = string.gsub ("%1", ",", "")
print (num)  --> 123456


Finally, to do arithmetic on it you should probably do "tonumber" on it, to make sure it is being considered as a number:


num = tonumber (string.gsub ("%1", ",", ""))
print (num) --> 123456


#2
finally, I figured out I should call script function rather than setvariable under trigger...
it's so inconvenience.

and any idea to trim out the "," sign?
#3
nice!!
thx nick
noob mistake, :P
everything just working perfect