Using RegExp to Parse a List

Posted by Terry on Sat 25 Oct 2008 07:26 PM — 4 posts, 20,140 views.

USA #0
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?
USA #1
<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.
USA #2
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?
USA #3
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.)