I omitted the /g on the regex in the sample code, but the working code has it.<br><br>The working code would deal with more complex patterns using a combination of letters, numbers and special characters, for example MAC/IP addresses, hostnames, etc. It could be any pattern of characters really, but they would have to be replaced in the same way that Mike suggested, where the surrounding data is not affected.<br>
<br><br><br><div class="gmail_quote">On Wed, Dec 22, 2010 at 12:54 PM, J Z Tam <span dir="ltr"><<a href="mailto:jztam@yahoo.com">jztam@yahoo.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td style="font-family: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; font-size: inherit; line-height: inherit; font-size-adjust: inherit; font-stretch: inherit;" valign="top">
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/<div class="im">
<br>sorta, replaces stuff between patterns also<br><br></div> 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><<a href="mailto:richard.dice@gmail.com" target="_blank">richard.dice@gmail.com</a>></i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;">
<br>From: Richard Dice <<a href="mailto:richard.dice@gmail.com" target="_blank">richard.dice@gmail.com</a>><br>Subject: Re: [tpm] Search/Replace
multiple patterns in a single line, in a single pass?<br>To: "J. Bobby Lopez" <<a href="mailto:jbl@jbldata.com" target="_blank">jbl@jbldata.com</a>><br>Cc: "Toronto Perl Mongers" <<a href="mailto:tpm@to.pm.org" target="_blank">tpm@to.pm.org</a>><br>
Received: Wednesday, December 22, 2010, 12:20 PM<div><div></div><div class="h5"><br><br><div>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 href="http://mc/compose?to=jbl@jbldata.com" target="_blank">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 href="http://mc/compose?to=toronto-pm@pm.org" target="_blank">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 href="http://mc/compose?to=toronto-pm@pm.org" target="_blank">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></div></div></blockquote></td></tr></tbody></table><br></blockquote></div><br>