in reply to Re: Composing regex's dynamically
in thread Composing regex's dynamically

Interesting. Does the wrapping of a compiled regex with the remembered modifiers cost more than adding plain non-capturing parens that you would need anyway? Or in your situation did you not need parens at all?

I wonder if composing actual pre-compiled regex's is any better than converting to string and re-compiling?

Replies are listed 'Best First'.
Re^3: Composing regex's dynamically
by JavaFan (Canon) on Apr 28, 2011 at 22:28 UTC
    I've never researched that, but if I make building blocks to assemble regexes, I typically surround that with (?:), unless it's not going to matter. So, I'd write:
    my $bblock1 = "[a-f]"; my $bblock2 = "foo"; my $bblock3 = "(?:$bar|$baz)";
    And then there's:
    my $bblock4 = "[a-i]"; my $bblock5 = "[A-I]"; my $bblock6 = "[0-9]"; my $bblock7 = "$bblock4|$bblock5|$bblock6"; $bblock7 =~ s/\Q]|[\E//g; # Make one range
      Why are the dollar signs doubled?

      I would not expect the -xism part to cost anything over the (?: ) without that. Again, that makes me wonder if re-compiling the string is worse than incorporating a compiled regex in another qr. Naturally, being able to leave off the parens completely simplifies things.

      The regex engine has improved its optimizations in successive versions of Perl too; I think I read something about that in either the 5.10 or 5.12 notes.

        Why are the dollar signs doubled?
        Typos. Fixed.
        Again, that makes me wonder if re-compiling the string is worse than incorporating a compiled regex in another qr.
        It's certainly not *worse*. Note that if you do:
        my $re1 = qr/foo/; my $re2 = qr/bar/; my $re3 = qr/$re1|$re2/;
        you compile three patterns, and stringify two. If you write:
        my $re4 = qq/foo/; my $re5 = qq/bar/; my $re6 = qr/$re4|$re5/;
        you compile only one pattern, and stringify none.