Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

On the regex pattern variable to be inserted into another

by Anonymous Monk
on Feb 09, 2022 at 15:00 UTC ( [id://11141274]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Anyone kind enough confirming whether the regex pattern variable to be inserted into another really has been compiled , so not merely its raw string inserted which, after the insertion, all raw string sum is compiled wholly as first time ?
  • Comment on On the regex pattern variable to be inserted into another

Replies are listed 'Best First'.
Re: On the regex pattern variable to be inserted into another
by bliako (Monsignor) on Feb 09, 2022 at 16:12 UTC
    ref(qr/a.*u/) eq 'Regexp'

    bw, bliako

Re: On the regex pattern variable to be inserted into another (updated)
by LanX (Saint) on Feb 09, 2022 at 15:37 UTC
    I can confirm that your question is fuzzy and hard to decipher.

    Code please, at best an SSCCE

    see also I know what I mean. Why don't you? from brother father Grand

    update

    probably you mean the difference between

    • my $str = 'a.*u'; m/$str/i
    and
    • my $pc = qr/a.*u/; m/$pc/i
    ?

    see qr for more

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: On the regex pattern variable to be inserted into another
by NERDVANA (Deacon) on Feb 10, 2022 at 16:15 UTC
    I will confirm the opposite.

    If you have variable  $foo= qr/a.*b/ and you plug that into another regex like  my @matches= /$foo/g it will re-compile the regex just to add the /g switch which doesn't even change the content of the regex, and it will re-compile every time it hits that line of code.

    I discovered that while writing my Language::FormulaEngine::Parser where I wanted to take author-supplied regexes and then test them against the current position of the input. If you want these to stay pre-compiled, you need to eval the whole sub where they get used and then call that coderef.

    Edit: I was wrong, perl does have special cases for  m/$foo/ and  m/$foo/g and  s/$foo/.../ to avoid re-compiling the regex. However, anything more than that like changing the anchor  /\G$foo/gc will trigger a re-compile.

      > /$foo/g

      I expect m/$foo/ to "recompile" because it could also be m/$foo$bar/

      The point of qr is that the inside of $foo=qr// is not effected.

      If you don't want it to be recompiled use $str =~ $foo without surrounding m/.../

      Your problem seems to arise from the /g "global" switch which is actually changing the whole m operator and not the regex.

      Please note that the documentation for qr/STRING/msixpodualn doesn't list /g !

      It might help to know that m// and m//g are realized as separate commands in other languages.°

      update

      °) e.g. in JS

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        Yes, but it is perfectly reasonable to expect that Perl might recognize  /$foo/ or  /$foo/g as being able to re-use a compiled expression instead of compiling a new one. This is what I think the OP was asking, and I am confirming that no, Perl does not recognize these special cases. The only way to take an unknown regex and efficiently plug it into code that does anything more elaborate than  ... =~ $foo (and gets called more than once, because it will only matter if the code gets called in a loop or something) is to eval an entire sub with the regex expanded within it.
        It might help to know that m// and m//g are realized as separate commands in other languages.

        Yes and in those languages, you can usually create an object to represent the compiled regex and then get either/both operations out of one compilation. Unfortunately perl can't do this easily.

        Edit: Perl does special-case these, actually. Just not anything more elaborate like prefixing  /\G$foo/ to change the anchor.

Re: On the regex pattern variable to be inserted into another
by BillKSmith (Monsignor) on Feb 10, 2022 at 00:10 UTC
    I suspect that you have a regex with a fixed a variable part.
    use strict; use warnings; my $string = 'title: ABC_II'; foreach my $index (qr/I/, qr/II/, qr/III/) { print "Match\n" if $string =~ m/ABC_$index/; }

    You want to know if you can speed it up by pre-compiling the fixed part.

    use strict; use warnings; my $string = 'title: ABC_II'; my $fixed = qr/ABC_/; foreach my $index (q(I II III)) { print "Match\n" if $string =~ m/$fixed$index/; }

    Clearly the syntax is allowed. You would have to benchmark your case to find out if and how much it helps.

    Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11141274]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-19 21:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found