3
votes

I am new to lua scripting language so just trying my hand into this language. I got one task which will parse browser user agent string and will return browser information.

Though i have done lot of R&D on lua to get solid LUA library which does this job but unfortunately i have not found anyone.

So i tried to implement it through by using some PHP UA agent library logic into my lua script. As per PHP library (php us parser) it has its own regex file for all probable user agent string so it actually store all these string in JSON file and match incoming UA string with these regex file data and returned the complete details for incoming user agent.

Now i am also trying to replicate same logic in my lua script, but unfortunately, since lua does not have it own regex library, I am trying to parse UA string with its existing available function. Now I'm stuck here to implement the logic.

here is my UA string

local ua ="Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"

and here is regex code for this string

local pattern =    [[@(Chromium|Chrome)/(\d+)\.(\d+)(?:\.(\d+))?@]]

here is my code get the exact match from UA string

for w in s:gmatch(pattern) do
    ngx.say(w)
end`

after running this code it return me nil or NO values

even i have tried with this pattern also local pattern = [[(Chrome|Chromimum)/%d+]

Now its returning me only only one one match that is "Chrome" where as it should return result like this

[0] => Chrome/39.0.2171
[1] => Chrome
[2] => 39
[3] => 0
[4] => 2171

Where each index represent browser different values like browser name, version,os name etc.

Any help is much appreciated.

1

1 Answers

1
votes

Lua patterns are not the same as regex. The following regex:

(Chromium|Chrome)/(\d+)\.(\d+)(?:\.(\d+))?

would be rewritten as (note that the | is invalid in lua patterns):

(Chrom[eium]+)/(%d+)%.(%d+)%.?(%d*)

You can see the above at work here.