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

reg. expr. multiple substitutions

by silentius (Scribe)
on May 17, 2012 at 13:18 UTC ( #971059=perlquestion: print w/ replies, xml ) Need Help??
silentius has asked for the wisdom of the Perl Monks concerning the following question:

Revered Monks,

For each input string $str I have to test for several possible substitutions, say:

if $str contains tag1 then replace tag1 (in $str) by subst1;

if $str contains tag2 then replace tag2 by subst2;

and so on, and I have 49 such (tag, subst) pairs.

My question, dear and revered and full of wisdom monks, is: could you enlighten me as to a "good" way of programming this?

I mean, do I have any better choice than writing 49 lines like these?:

$str =~ s/tag1/subst1/; $str =~ s/tag2/subst2/; $str =~ s/tag3/subst3/; ... $str =~ s/tag49/subst49/;

At least, is there any syntactic sugar for those 49 lines?

Humbly and in much appraisal,
silentius

Comment on reg. expr. multiple substitutions
Select or Download Code
Re: reg. expr. multiple substitutions
by BrowserUk (Pope) on May 17, 2012 at 13:26 UTC

    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?

      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?

Re: reg. expr. multiple substitutions
by Eliya (Vicar) on May 17, 2012 at 14:06 UTC

    You could alias $_ to $str, which would allow you to at least get rid of the repetitive "$str =~ " part in every line.

    Other than that, you do have to declare somehow what to replace with what, and this get can hardly get any more compact than a list of s/.../.../ (unless there is some pattern or regularity in the tag-substitution pairs that would allow you generate them automatically — but I suppose the ones given are not the real ones).

    for ($str) { s/tag1/subst1/; s/tag2/subst2/; s/tag3/subst3/; ... s/tag49/subst49/; }
Re: reg. expr. multiple substitutions
by dulwar (Scribe) on May 17, 2012 at 16:53 UTC

    Use a hash:

    my %subs = ( tag1 => 'subst1', tag2 => 'subst2', ... ); my $pattern = join('|', keys %subs); $str =~ s/($pattern)/$subs{$1}/g;

    It would be a good idea to simplify $pattern if there is some common pattern to the tags.

    By the way, you need to use the g modifier to replace all occurrences of each tag if it can occur more than once in the string, also if you are substituting each tag separately.

Re: reg. expr. multiple substitutions
by silentius (Scribe) on May 17, 2012 at 18:18 UTC
    Thank you very much once again, dear monks.

    Your words have been useful.

    Wishing all the very best,
    humbly and thankfully,
    silentius

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2014-11-01 07:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (228 votes), past polls