Removing commas from trigger catches

Posted by Silencher on Wed 25 Mar 2015 05:58 AM — 9 posts, 36,786 views.

#0
Hello.

I'm trying to make a trigger to save my bank balance because for some reason you can only see your bank balance inside the bank which disturbs me.

Here's the sample output:

Total Gold in Bank: 18,231

Below is my trigger:

<triggers>
<trigger
enabled="y"
group="Banker"
match="Total Gold in Bank: *"
send_to="12"
sequence="100"
>
<send>wealth = %1</send>
</trigger>
</triggers>


---------
The trigger only catches the '18' though and so I'm stuck as to what to do. I want to catch the whole 18,231, remove the comma, then store the 18231 number (Which will fluctuate of course) into that wealth variable. I have another simple alias that just notes that variable's contents.

How do I catch that? I'm assuming I need to use a regular expression but I'm not familiar with those.
Australia Forum Administrator #1

wealth = "%1"
#2
That doesn't resolve my issue if I need to do arithmetic.

My desire is to make a trigger that totals my on-hand gold and my banked gold. Ordinarily I would just add 0 to the variable to convert it to a number, but that comma is still causing me to not catch the 2nd part of the gold amount.

It does allow me, of course, to see the string version, with the comma now though. But that's not really the overall goal.
USA Global Moderator #3
wealth = tonumber(string.gsub("%1", ",", ""))
Amended on Wed 25 Mar 2015 07:02 PM by Fiendish
USA #4
You can use regular expressions, and a bit of creativity. Or take the easy road and use scripting.


But just FYI, this regular expression should work for you.

^Total Gold in Bank\: ((\d+)\,|)((\d+)\,|)(\d+) $


Should work for anything between 0 and 999,999,999

Then just wealth = "%2%4%5"


http://www.mushclient.com/mushclient/regexp.htm
For more info.
#5
With the 'tonumber' example I get this error:
I get an error: bad argument #2 to 'tonumber' (base out of range)


And the regex solution doesn't even cause the trigger to fire. Does it matter that there are (varying) spaces between the : and the 1st number of the total gold?

UPDATE: I fixed the 'tonumber' example, it just needed another set of parenthesis. The regex solution still isn't firing, and I'm not sure why. The box for regular expressions is checked, of course.
Amended on Fri 27 Mar 2015 04:22 AM by Silencher
Australia Forum Administrator #6
Every space is important, but you can do something like this to skip one or more spaces:

\s+
USA #7
As nick said, spaces matter.

If his tip doesn't fix the problem i would look at the extra space at the end. It seemed odd to have one there but that is what your example had.


Total Gold in Bank:118,231 
Total Gold in Bank: 18,231 
Total Gold in Bank:  8,231 

If your output looks anything like this, then Nick's tip is the way to go.


Total Gold in Bank: 118,231
Total Gold in Bank: 18,231
Total Gold in Bank: 8,231

If it looks like this however. My guess is an extra space got added at the end. And needs to be removed.

(d+) $
(d+)$

for example.

Without more details it is hard to say though.
#8
I just threw the \s+ before the string, after the : and after the # and that seemed to work. Both ways work, so that's great, thank you!