Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Regular expression question

by TASdvlper (Monk)
on Jan 15, 2004 at 21:27 UTC ( [id://321676]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all,

Is there some way to combine the following 2 regex into one ?

$wrapper =~ s/\</&lt;/g; $wrapper =~ s/\>/&gt;/g;
actually, this particular example does 2 unique substitutions, how would you handle multiple substitutions (in conjnction with the above regex) ? e.g.
$wrapper =~ s/foo/bar/g; $wrapper =~ s/bar/foo/g; ...
Thanks all.

Replies are listed 'Best First'.
Re: Regular expression question
by Abigail-II (Bishop) on Jan 15, 2004 at 21:32 UTC
    If you want to combine all four, you could do:
    my %repl = qw / < &lt; > &gt; foo bar bar foo /; $wrapper =~ s/([<>]|foo|bar)/$repl{$1}/g;
    But other solutions are possible as well.

    Abigail

      Thanks. So to expand on this a bit, I want to do that regex but not if there is a
      between the angle brackets (there probably never be white space, before and after the br, but I want to check just in case). so,
      <br> Don't do the regex < br > Don't do the regex < br> Don't do the regex <br > Don't do the regex <this> Do the regex <is> Do the regex <a> Do the regex <test> Do the regex <this is a test> Do the regex
      Thanks again ...
        #!/usr/bin/perl use strict; use warnings; undef $/; $_ = <DATA>; my %repl = qw / < &lt; > &gt; foo bar bar foo /; s/(<\s*br\s*>|[<>]|foo|bar)/$repl{$1}||$1/ge; print; __DATA__ <br> Don't do the regex < br > Don't do the regex < br> Don't do the regex <br > Don't do the regex <this> Do the regex <is> Do the regex <a> Do the regex <test> Do the regex <this is a test> Do the regex <br> Don't do the regex < br > Don't do the regex < br> Don't do the regex <br > Don't do the regex &lt;this&gt; Do the regex &lt;is&gt; Do the regex &lt;a&gt; Do the regex &lt;test&gt; Do the regex &lt;this is a test&gt; Do the regex

        Abigail

Re: Regular expression question
by Roy Johnson (Monsignor) on Jan 15, 2004 at 21:48 UTC
    First, you may want to look at a module like HTML::TextToHTML to take care of this particular type of example.

    But for a general case, it would be something like this (the sort is to prevent short matches like f from usurping longer ones like foo)

    my %repls = qw( < &lt; > &gt; foo bar bar foo f baz ); my $replstr = join '|', sort {length $b <=> length $a} keys %repls; s/$replstr/$repls{$&}/g;
    Update: Shamelessly stole Abigail's initialization syntax

    The PerlMonk tr/// Advocate
Re: Regular expression question
by BrowserUk (Patriarch) on Jan 15, 2004 at 21:54 UTC

    Another way.

    $re = qr[ ( the(?{$rep='The'}) | quick(?{$rep='Quick'}) | brown(?{$rep='Brown'}) | fox(?{$rep='Fox'}) ) ]x; $s = 'the quick brown fox'; $s =~ s[$re][$rep]g; print $s; The Quick Brown Fox

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Timing (and a little luck) are everything!

      You are one evil, sick, and twisted individual.

      Just thought someone should let you know.

      ++

      update:lol


      ---
      demerphq

        First they ignore you, then they laugh at you, then they fight you, then you win.
        -- Gandhi


        Your right. I should have added a little more whitespace to clarify things :)

        $re = qr[ ( the (?{ $rep = 'The' }) | quick (?{ $rep = 'Quick' }) | brown (?{ $rep = 'Brown' }) | fox (?{ $rep = 'Fox' }) ) ]x;

        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        Timing (and a little luck) are everything!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-03-29 07:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found