Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: reg. expr. multiple substitutions

by BrowserUk (Pope)
on May 17, 2012 at 13:26 UTC ( #971061=note: print w/ replies, xml ) Need Help??


in reply to reg. expr. multiple substitutions

Use a hash and a loop:

my %subs = ( tag1 => 'subst1', tag2 => 'subst2', ... ); $str =~ s[$_][$subs{$_}] for keys %subs;

Depending upon the nature of the text involved, you may need to quotemeta some stuff, but that's the basic idea.

Note however, this is only slightly more compact; it will not be quicker or much easier to maintain.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?


Comment on Re: reg. expr. multiple substitutions
Download Code
Re^2: reg. expr. multiple substitutions
by kennethk (Monsignor) on May 17, 2012 at 14:34 UTC

    To avoid the quotemeta problem and allow a bit more flexibility, it might make sense to use qr// as described in Regexp Quote Like Operators.

    my %subs = ( qr/tag1/ => 'subst1', qr/tag2/ => 'subst2', ... ); $str =~ s[$_][$subs{$_}] for keys %subs;

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      That would probably work, but I'm never quite sure that there is any benefit to pre-compiling a regex with qr// if you are going to then interpolate that into the m// or s/// construct to use it.

      With m//, you can do: my $re = qr/.../; $string =~ $re; thus avoiding re-interpolating the compiled regex, but there is no way to do that with s///.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

        My argument is not so much about performance, as it is about avoiding escaping problems and allowing ism modifiers. Since hash keys get stringified, keying off regexes already compromises any benefit from compiling. I would also expect readability would be improved if extensive escaping is necessary.

        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

        That would probably work, but I'm never quite sure that there is any benefit to pre-compiling a regex with qr// if you are going to then interpolate that into the m// or s/// construct to use it.

        Especially when the use as a hash key converts it back to a plain string anyway.

        But, what am I missing about the benefit over quotemeta? Iff you want to have a replacement like 'a[0]' => 'a[1]', you either have to run it through quotemeta or escape it by hand if you want to use qr//, don't you?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2014-10-22 22:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (122 votes), past polls