Trigger match string for a repeating list of varying length

Posted by Kevnuke on Thu 18 Jul 2019 11:23 AM — 7 posts, 27,976 views.

USA #0
I'm trying to create a trigger for a plugin that captures the list of enemies for my current city and adds them to an array. cityenemies.cyrene = {}, in this case.

Here is a shortened example of what I see when I do CITY ENEMIES in Achaea.


Enemies of the City of Cyrene:
Rip, Achilles, Terrin'tuuran, Crythril, Jems, Ama-maalier, Artanis, Jack, Tesha, Cooper
Total: 10


Currently I'm testing using a world trigger with this regex pattern:
^((, )?([A-Z][-'a-z]*))*$

And it works, surprisingly matching only on the list of names and not the line before and after it. In reality the list is about 200 names long and can be much longer. I read somewhere that the limit for wildcards in triggers is 36.

So how do I turn that list of potentially hundreds of names into something like this:

cityenemies.cyrene = {"Rip", "Achilles", "Terrin'tuuran", "Crythril", "Jems", "Ama-maalier", "Artanis", "Jack", "Tesha", "Cooper"}


so that I can compare them to a string value later?

After looking at that pattern I realize that every even numbered wildcard would be ", ". How do I group the comma and the space together without parenthesis so that it always matches either both or neither before a name without capturing them?

[EDIT] Corrected a few typos.
Amended on Sat 20 Jul 2019 06:45 AM by Kevnuke
USA Global Moderator #1
Capture the whole line together and then split on ", " afterwards using one of the functions from http://lua-users.org/wiki/SplitJoin
Amended on Thu 18 Jul 2019 12:18 PM by Fiendish
USA #2
Thanks Fiendish. Looks like a split function would do that. I didn't see where one was defined though or is that a method of the string library now? Should i just use gmatch with the pattern I used to match names that I already have?
USA Global Moderator #3
Did you read my link?
Australia Forum Administrator #4
Template:post=6079
Please see the forum thread: http://gammon.com.au/forum/?id=6079.
Australia Forum Administrator #5
Near the end of that link Fiendish gave a rather short and neat function:


function string:split(pat)
   local fields = {}
   local start = 1
   self:gsub("()("..pat..")", 
      function(c,d)
         table.insert(fields,self:sub(start,c-1))
         start = c + #d
      end
   )
   table.insert(fields, self:sub(start))
   return fields
end
USA #6
oh yea I read a lot of it, just hadn't gotten that far and I wasn't sure which one was the best solution. I tried some and all but the first name had a leading space, so I wrote this:


function names_to_array (namelist)

    local t = {}
    local pattern = "[A-Z][-'a-z]*"

    for str in string.gmatch(namelist, pattern) do
        table.insert(t, str)
    end

    return t
end


I just pass in the entire matching line from the trigger like Fiendish suggested.

Thank you for the new link. I'll read that too.