chaoslawful/lua-nginx-module — pull request #182 by raybejjani (2012-12-03 18:03:52+00:00)
Title: re named patterns

Hi Agentzh,
A project I'm working on requires named pattern support from PCRE. I noticed the lua wrapper doesn't export this and I've started working on it. This is still a work in progress but I thought I'd ask for your feedback.
I've implemented an optional return of the mapping of named patterns to matched pattern numbers. If a ctx is passed to re.match, and it contains a named_patterns field that is a table, it will be populated with named pattern -> match number.
E.g. matching "I am a (?.*)pot, full of (?.*)" against "I am a teapot full of green tea"
yields
[0] => I am a teapot full of tea
[1] => tea
[2] => grean tea

where ctx.named_patterns => Table {
[pottype] => 1
[fullwith] => 2
}

Duplicate name support is enabled with a "r" option, but not handled otherwise. I'm not sure if there's a better letter to use here. "d" was already taken.

My end requirements are that I also support duplicate names but I'm not sure how best to expose this. I can think of 2 possibilities:
1- Return only the named pattern -> number mapping for the matching named pattern. This assumes only one duplicate matches.
2- Return an array in the name -> number mapping. This may be onerous since the common case is to not have duplicate names.
3- Conditionally do (2) when the duplicate name option is used. This may cause more confusion and errors since the types go from int to table in ctx.named_patterns.