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

Re: Regexp substitution using variables

by jwkrahn (Monsignor)
on Nov 25, 2020 at 19:49 UTC ( #11124219=note: print w/replies, xml ) Need Help??

in reply to Regexp substitution using variables

For certain flags you can do:

$value =~ s/(?$flags:$pattern)/$replacement/;

Replies are listed 'Best First'.
Re^2: Regexp substitution using variables
by Bod (Friar) on Nov 25, 2020 at 23:17 UTC

    This answer and other answers have suggested:

    $value =~ s/(?$flags:$pattern)/$replacement/;
    In my answer I used a subtle variation:
    $value =~ s/(?$flags)$pattern/$replacement/;
    which worked as expected in my test code. So I went off to the documentation and sure enough it shows both but it does not (at least to my eyes) show what the difference is between them. Can anyone explain if there is a difference and when it practically matters? It doesn't seem to matter here.

    On a different note - is it preferred by other Monks that questions like this get asked in the thread or is the preference for them to have their own new thread?,

      > does not (at least to my eyes) show what the difference is between them.

      you are comparing

      in your example there is no difference, but in the second approach with pattern the reach of modifiers is limited to the group.

      DB<24> p 'xX' =~ /(?i:X)X/ 1 DB<25> p 'xX' =~ /(?i:X)x/ DB<26> p 'xX' =~ /(?i)Xx/ 1 DB<27>

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11124219]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2021-01-21 00:00 GMT
Find Nodes?
    Voting Booth?