[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Using RegExp to Parse a List

Using RegExp to Parse a List

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Terry   USA  (87 posts)  Bio
Date Sat 25 Oct 2008 07:26 PM (UTC)
Message
I'm trying to parse a list of bank accounts in a trigger. It has to match something like:
Quote:
Account Amount
<5-digit acct#> <number>


Here is what I have so far:
"^Account(?: +)Amount(?: +)\n(\d{5})(?: +)(?:\d+)(?: +)$"


The problem is that I don't know how many bank accounts there will be in the list at any given time, so the second line has to be recursive.... Also, I would like to put the list of account numbers into an array. Would anyone be able to help me?
Top

Posted by WillFa   USA  (525 posts)  Bio
Date Reply #1 on Sat 25 Oct 2008 07:42 PM (UTC)
Message
<triggers>
<trigger
enabled="y"
match="^(?'AcctNum'\d{5}) ++\d+ ++$"
name="AcctGrab"
regexp="y"
send_to="12"
sequence="100"
>
<send>Accts[#Accts +1] = %&lt;AcctNum&gt;</send>
</trigger>
<trigger
enabled="y"
lines_to_match="2"
match="^Account ++Amount ++$"
name="AcctStart"
regexp="y"
send_to="12"
sequence="100"
>
<send>Accts= {}</send>
</trigger>
</triggers>


The above depends on your scripting language being Lua, however.

You can make it a little more robust by adding a line to EnableTrigger("AcctGrab", true) in the AcctStart Trigger, and EnableTrigger("AcctGrab", false) in your prompt so that the array doesn't get bad data that's formatted the same way.
Top

Posted by Terry   USA  (87 posts)  Bio
Date Reply #2 on Sat 25 Oct 2008 07:53 PM (UTC)
Message
Hrm, would you be willing to explain what you did? :P
I see that you made a trigger for each line... But what was the regexp itself that you used?
Top

Posted by WillFa   USA  (525 posts)  Bio
Date Reply #3 on Sun 26 Oct 2008 12:19 AM (UTC)

Amended on Sun 26 Oct 2008 12:29 AM (UTC) by WillFa

Message
The AcctStart trigger:
has a pattern of "^Account ++Amount ++$"
Which means find From the start of line("^") "Account" plus one or more spaces (" "), "atomically" (greedily grab all the spaces, and don't bother rechecking if fewer spaces will match the pattern) ("++") followed by "Amount" and atomically grabbed spaces (" ++") until the end of line ("$").

The Send is being sent to the Scripting engine specified in the Send To field, and it sets a variable (scripting only, it's not saved) to an empty table. -> " Accts = {} "

the AcctGrab trigger:
has a pattern of "^(\d{5} ++\d+ ++$", which is, start of line ("^"), capturing 5 digits as the named parameter AcctNum, ("(?P<AcctNum>\d{5})"), atomic spaces again (" ++"), and a series of any numbers ("\d+"), and an atomic space grab again (" ++"), and then end of line ("$")

It's script set's the Table ("Acct") value, at index size of the table + 1 ("[#Accts + 1]"), equal to (" = "), The named capture parameter (" %<AcctNum>" )

AcctGrab will fire on every line that fits this format. If your mud sends something like:
Account          Amount
12345           100000
98765           10

Movement        To Next Lvl
10725           175324


The Accts table will end up containing:
Accts[1] = 12345
Accts[2] = 98765
Accts[3] = 10725


So that's why I recommended EnableTrigger("AcctGrab", true) during AcctStart, and disabling it in your prompt or a line that concludes the account listing.

You don't need to use non-capturing groups for spaces ("(?: +)"), the regexp engine doesn't treat a space as a delimiter or anything. " +" is just as valid.
In your original pattern, It'd only match the header line and the first line. To match additional account lines, you'd need 2 triggers.

((If you use captured parameters directly in the send to field, like this, be aware that MushClient does the capture substitution before sending the code to the Lua engine to be compiled. So the source that Lua evaluates ends up being:

Pattern: (%?P<Named>\d+)
Send To: Scripting
Send:  foo = %<named>   
Sample Line: "       500     "
Effective source:  foo = 500
(this is fine.)

Pattern: (%?P<Named>\w+)
Send To: Scripting
Send:  foo = %<named>   
Sample Line: "       Bob     "
Effective source:  foo = Bob
(this is ends up looking to set foo to a variable named Bob. Probably not what you want.)

Pattern: (%?P<Named>\d+)
Send To: Scripting
Send:  foo = "%<named>"   
Sample Line: "       Bob     "
Effective source:  foo = "Bob"
(this is probably the result you want.)


Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


15,165 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]