Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string.match and regex in Lua, is it possible?

I'm currently working on a script gathering information from a website. I'm wondering if string.match would be able to accept regex pattern matching, such as the following:

string.match(page, "(?:(Phrase One|Phrase Two|Phrase Three|...))")

The problem is, I don't know if it can be done, but I'm willing to try any solutions. Thanks!

like image 971
Josh Avatar asked Oct 21 '25 10:10

Josh


2 Answers

You can use the Lrexlib if you need full blown regular expressions.

like image 62
sylvanaar Avatar answered Oct 23 '25 23:10

sylvanaar


What you could do is to define a function which takes your patterns as a table and then returns a table of matches, or if you only need to know if the text is contained in the page then following would work.

    function FindAny(strContent,tblPatterns)
    bReturn = false
    for i,strPattern in ipairs(tblPatterns) do
        if (string.find(strContent,strPattern) ~= nil) then
            bReturn = true
            break
        end
    end
    return bReturn
end
if FindAny('pattern 4',{'pattern 1','pattern 2','pattern 3','pattern 4'}) then
    print('Pattern Found')
end
like image 42
Jane T Avatar answered Oct 23 '25 23:10

Jane T