in reply to Parameterized Regex

What you demonstrated is called named captures, in Perl. In Python, it's called named groupings. I think that parameterized regexes are different.

sub match { my $args = shift; my $entity = $args->{entity}; my $quantity = $args->{quantity}; return $args->{target} =~ m/^(?:$entity){$quantity}/; } match ({entity => 'a', quantity => 5, target => 'aaaaa'}) && print "Ma +tch\n";

This is a little verbose, and wholly contrived, but the idea here is that you can pass into the regex things like metacharacters and quantifier totals, thus treating the regexp as a general template and letting the variables that get interpolated into it as specializations of the template. In this way, the regexp is accepting parameters, or is parameterized.

This use of parameterized seems consistent with other examples where this term, or the term parametric are used. Parametric templates in C++, for example, which allow for generic functionality to be specialized by binding parameters to a template.

I did some searching online on the name parameterized regex. The search results found a few hits that support my notion, and then one hit that presented a research paper on parameterized regexes. That paper was completely different from this explanation, diving into set theory and NFA and DFA state machines. So it's possible that casual use has rendered the term overloaded.


Dave

Replies are listed 'Best First'.
Re^2: Parameterized Regex
by Laurent_R (Canon) on Mar 03, 2017 at 07:34 UTC
    If you use a named capture, then you can use that capture for a subsequent match, perhaps a new regex. BTW, this gets you closer to a possible grammar. Maybe using a named capture this way in a new regex is what is meant by a parametrized regex.
      If you use a named capture, then you can use that capture for a subsequent match, perhaps a new regex. ... using a named capture this way in a new regex ...

      Could you give an example of what you mean? AFAIU, the  %+ hash is re-initialized when a new regex match is entered.

      c:\@Work\Perl\monks>perl -wMstrict -le "my $s = 'one two three four'; ;; $s =~ m{ \A (?<save> \w+) \s+ (?<this> \w+) \s+ (?<for> \w+) \s+ (?<later> \w+) \s* \z }xms; print join '-', @+{ qw(save this for later) }; ;; $s = 'fee fie foe fum'; ;; $s =~ m{ (?{ print join '==', @+{ qw(save this for later) } }) \A (?<save> \w+) \s+ (?<this> \w+) \s+ (?<for> \w+) \s+ (?<later> \w+) \s* \z }xms; print join '+++', @+{ qw(save this for later) }; " one-two-three-four Use of uninitialized value in join or string at (re_eval 1) line 1. Use of uninitialized value in join or string at (re_eval 1) line 1. Use of uninitialized value in join or string at (re_eval 1) line 1. Use of uninitialized value in join or string at (re_eval 1) line 1. ====== fee+++fie+++foe+++fum
      To do something like you suggest, you would have to "capture the capture" in a separate set of variables before the new match was entered.


      Give a man a fish:  <%-{-{-{-<

        Yes, I guess you're most probably right that %+ hash is probably lost when you enter a new regex. Although my initial point was really about using the named capture within the same regex.

        Besides the fact that I wrote my previous post on a mobile device while commuting to work, with no chance to test, I guess I got carried away by the fact that I am working much more on Perl 6 than on Perl 5 these days when it comes to these matters.

        Having said that, I still believe that named captures can bring you something (limited) in that direction. But too late now, I'll try to come up with something tomorrow.

Re^2: Parameterized Regex
by QM (Parson) on Mar 03, 2017 at 10:11 UTC
    Thanks. That's exactly what I would expect, based on the name "parameterized regex". I have used that approach many times.

    Since my OP, the purveyor of the poorly chosen phrase has admitted the name was chosen in haste, and he may change it. Besides the normal clutter of imprecise language from native speakers, the primary developers on the software in question are not native speakers. I always try to give non-native speakers more slack. (But marketing types and others, whose job it is to find the right words, get the sharp edge of the dictionary.)

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of