string.find(string, pattern|pattern) -- ????

Posted by ThumpieBunnyEve on Sat 16 May 2009 07:00 PM — 3 posts, 22,016 views.

#0
No matter what i do in a Lua script, i cant get a pattern to include an OR.


solo = "http://ggg.cnn.com http://www.cnn.com www.cnn.com monkey-tribble-duck w.cnn.com w  ww oog.nomface.com www wwww w|www wW  W WW WWW  ouote o.ote.com.nom ___ gggg quote q.ote.com.nom.con WWWW  WWW. http://www.cnn.com"

luke = "([hHtTpPsSmMaAiIlLoO]+://[%S]*[%w#/])|([wW]+[wW]+[wW]+[\.])"

leia = "[hHtTpPsSmMaAiIlLoO]+://[%S]*[%w#/]|[wW]+[wW]+[wW]+[\.]"

r2d2 = "(([hHtTpPsSmMaAiIlLoO]+://[%S]*[%w#/])|([wW]+[wW]+[wW]+[\.]))"

c3p0 = "([hHtTpPsSmMaAiIlLoO]+://[%S]*[%w#/])*([wW]+[wW]+[wW]+[\.])*([hHtTpPsSmMaAiIlLoO]+://[%S]*[%w#/])"

jaba = "(([hHtTpPsSmMaAiIlLoO]+://[%S]*[%w#/])*([wW]+[wW]+[wW]+[\.])*([hHtTpPsSmMaAiIlLoO]+://[%S]*[%w#/]))"

boba = "[hHtTpPsSmMaAiIlLoO]+://[%S]*[%w#/]*[wW]+[wW]+[wW]+[\.]*[hHtTpPsSmMaAiIlLoO]+://[%S]*[%w#/]"

yoda = string.find(solo, luke) -- fails
yoda = string.find(solo, leia) -- fails
yoda = string.find(solo, r2d2) -- fails
yoda = string.find(solo, c3p0) -- fails
yoda = string.find(solo, jaba) -- fails
yoda = string.find(solo, boba) -- fails


I'm out of options here X_X;
There is no way to put an OR into the pattern of a string.find is there?
Amended on Sat 16 May 2009 07:02 PM by ThumpieBunnyEve
USA #1
Lua pattern matching isn't the same as PCRE. The rex library has all the pcre syntax you're looking for.

The simple answer to your question is "No. Use 'or' between 2 string.find calls" :)


yoda = string.find(solo, "[hHtTpPsSmMaAiIlLoO]+://[%S]*[%w#/]") 
   or string.find(solo, "www") or string.find(solo, "WWW")


Lua Pattern matching doesn't support an | as "OR", or {}s for repetition quantifiers (i.e. %w{2,3} doesn't match at least 2 but no more than 3 alpha chars.)

Lua patterns tend to run a bit faster, since they're more simplistic. The speed of the language generally matches or exceeds the speed of compiling a rex regexp.


function IsUrl (darth)
 local vader = darth:lower()
  if vader:find("^http://") or vader:find("^mailto://") 
      or vader:find("^www") then 
   return true
 end
end

That should accomplish what you're looking for.
Amended on Sat 16 May 2009 08:22 PM by WillFa
Australia Forum Administrator #2
You can also use LPEG to match on patterns with more complex conditions, like this or that.

http://www.gammon.com.au/forum/?id=8683