in reply to Re: Advanced techniques with regex quantifiers
in thread Advanced techniques with regex quantifiers

Good points.

It's just that when balancing elegance & performance & dependencies, in some cases I end up favouring the "one big regex" approach - assuming I can work around its technical limitations.

For example, in one current use-case I want to search a large input stream for occurrences of any of several "things", and extract relevant information from each match. This is not exactly a "parser" use-case (since I don't care about most of the input stream, and don't want to build a unified AST from it) but it shares some similarities.

In my prototype design (which seems to be working out fine so far), I do it by defining an array entry for each "thing" like so:

my @things = ( { parse => qr/ regex0 /sx, action => sub { ... } }, { parse => qr/ regex1 /sx, action => sub { ... } }, ... );

And then when the program starts, it dynamically compiles all of those individual regexes into a big branching one of the form:

qr{ (?| regex0 (*MARK:0) | regex1 (*MARK:1) | regex2 (*MARK:2) | ... ) }sx

...and starts matching this big regex against the input stream using a sliding window approach inspired by this old post by BrowserUk.

And whenever a match is found, it calls the corresponding action function roughly like so:

$things[$main::REGMARK]{action}->();

This approach is attractive to me because:

Each branch/thing can have its own capture groups etc, and process them in its action function.
However, if their parsing regexes need more complex quantified parts, then the advanced techniques I described above become necessary in order to be able to keep this design.

If those techniques were built-in instead of requiring scary embedded code blocks, I think I would use this kind of approach more often.