Expanding on this issue only slightly, there are times where you would want to use a regex operations like string.match string.find and you want to match with a table in a way that would search by priority.
How would people do that? (I may look into it my self actually.)
First, a clarification: Lua does not use regex but instead it's own pattern matching system. It may seem nit-picky, but its important to distinguish between them as they have relatively vast differences.
"search by priority" is a pretty loose request. What are you prioritizing? In pote_'s specific case, I think the most logical method of prioritization without getting too complicated would be to sort by length. Although you may be able to use default Lua functions such as string.gmatch to accomplish this, I personally would just create my own function something like this:
local function lengthSearch( text, tbl )
local maxLength = 0
local selected
for name, v in pairs( tbl ) do
if string.find( text, name ) and string.len( text ) > maxLength then
selected = name
end
end
return selected or false
end
The one glaring issue I see with this method is that if you have two elements in tbl of the same length that are found in text then the selected one is up to the mercy of the for loop. An extension could be to check if the length is greater or equal, store any equal length elements in a table, and choose the element by another method.
If you're looking for words then you might want to throw away any elements that are found in the middle of the text, e.g. choosing "what" over "hat" when searching in "whatever". There are so many possibilities that I don't think there is one (or even a few) general algorithm(s) to solve it. Your choice of sorting really depends on the application.
My answer is more of an algorithmic solution rather than a pattern solution. If you give me more details as to what you want to prioritize, I might be able to help you with creating a Lua pattern for it. Of course, using an algorithm might be better (again, heavily relies on your specific problem).
Hope I was some help, and sorry for rambling on.