On 8 June 2011 09:36, Dirk Laurie
<dpl@sun.ac.za> wrote:
On Wed, Jun 08, 2011 at 09:37:15AM +0200, steve donovan wrote:
> On Wed, Jun 8, 2011 at 12:46 AM, Igor Medeiros <
igorosbergster@gmail.com> wrote:
> > (systemScreen([1 .. 9])|systemAudio([1 .. 9]))
>
> OK, your problem is that Lua patterns do not do alternatives like
> this. So you will have to split the match into two patterns:
>
If you do it this way:
match = function (s,t,init)
if type(t)=="table" then
for _,pattern in ipairs(t) do
if string.match(s,pattern,init) then
return string.match(s,pattern,init)
end
end
return
else return string.match(s,t,init)
end
end
then match(s,t,init) behaves like string.match, but also allows
match(s,{"systemScreen\([1-9]\)","systemAudio\([1-9]\)"})
(Actually, it would be nice if the the definition of a pattern
could be extended to allow table arguments in this way to all
string functions. This is not a non-suggestion!)
Dirk
As always in programming there are many ways of skinning a cat and personally I would not use match for this exercise as I would not want to search the string twice. I would instead use string.find looking for 'system' then find the variants anchored at the location returned by the first find. So something like
function find_screen_or_audio(input)
local s,e = input.find('system')
local s1,e1 = input:find('^screen%([1-9]%)',e)
if s1 == nil then
s1,e1 = input:find('^audio%([1-9]%)',e)
end
if s1 return then input:sub(input:sub(s,e1) end
end