<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">I'm with Richard:<br>Why not just tr the line?<br> DB<1><br>main::(bobbyRegex1.pl:10): my $line = "*!*!* a *!*!* b *!*!* c *!*!*";<br><br> DB<1><br>main::(bobbyRegex1.pl:14): my $pattern = '(a)+\s.*(b)+\s.*(c)+\s'; # kinda/<br>sorta, replaces stuff between patterns also<br><br> DB<1> $expectedMatches = $line =~ tr/abc/def/;<br><br> DB<3> x $expectedMatches<br>0 3<br><br> DB<4> x $line<br>0 '*!*!* d *!*!* e *!*!* f *!*!*'<br><br> DB<5><br><br>--- On <b>Wed, 12/22/10, Richard Dice <i><richard.dice@gmail.com></i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: Richard Dice <richard.dice@gmail.com><br>Subject: Re: [tpm] Search/Replace
multiple patterns in a single line, in a single pass?<br>To: "J. Bobby Lopez" <jbl@jbldata.com><br>Cc: "Toronto Perl Mongers" <tpm@to.pm.org><br>Received: Wednesday, December 22, 2010, 12:20 PM<br><br><div class="plainMail">Two thoughts.<br><br>Why aren't you using the 'global' switch on the substitution, like s///g ?<br><br>Second, if the substitutions in your real code are as simple as in your example code, you should consider the transliterate operator rather than the substitution operator, per $foo =~ tr///;<br><br>Sent from my iPhone<br><br>On 2010-12-22, at 10:44 AM, "J. Bobby Lopez" <<a ymailto="mailto:jbl@jbldata.com" href="/mc/compose?to=jbl@jbldata.com">jbl@jbldata.com</a>> wrote:<br><br>> Hey all,<br>> <br>> I'm trying to figure something out, but having some trouble. I'm trying to replace multiple patterns via capture buffers, with multiple replacements contained in a hash.<br>> <br>> Looking at the
code below, you can see that the patterns I'd like to match are contained in $regex just fine, however I'd like to have mutiple $replacement{$#} 's depending on the number of patterns I'm trying to replace.<br>> <br>> ==== begin-code ===<br>> #!/usr/bin/env perl<br>> use strict;<br>> use warnings;<br>> <br>> my %replacement;<br>> $replacement{'a'} = 'd';<br>> $replacement{'b'} = 'e';<br>> $replacement{'c'} = 'f';<br>> <br>> my $line = "*!*!* a *!*!* b *!*!* c *!*!*";<br>> <br>> #my $pattern = '(a)|(b)|(c)'; # Doesn't work, only matches first pattern<br>> my $pattern = '(a).*(b).*(c)'; # kinda/sorta, replaces stuff between patterns also<br>> <br>> #$line =~ s/$pattern/$replacement{$1}/; # <-- here's where I'm having difficulty<br>> $line =~ s/$pattern/$replacement{$1} $replacement{$2} $replacement{$3}/; # kinda/sorta<br>> <br>> #$line =~ m/$pattern/;<br>> #print "$1, $2, $3\n";<br>>
<br>> print $line."\n";<br>> ==== end-code ===<br>> <br>> <br>> Either I'm missing something, or this isn't actually possible, and I'll have to loop through the patterns and replace them individually.<br>> <br>> Thoughts?<br>> <br>> -Bobby<br>> _______________________________________________<br>> toronto-pm mailing list<br>> <a ymailto="mailto:toronto-pm@pm.org" href="/mc/compose?to=toronto-pm@pm.org">toronto-pm@pm.org</a><br>> <a href="http://mail.pm.org/mailman/listinfo/toronto-pm" target="_blank">http://mail.pm.org/mailman/listinfo/toronto-pm</a><br>_______________________________________________<br>toronto-pm mailing list<br><a ymailto="mailto:toronto-pm@pm.org" href="/mc/compose?to=toronto-pm@pm.org">toronto-pm@pm.org</a><br><a href="http://mail.pm.org/mailman/listinfo/toronto-pm" target="_blank">http://mail.pm.org/mailman/listinfo/toronto-pm</a><br></div></blockquote></td></tr></table><br>