Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

how to replace a matched pattern in a string with a different pattern of same length?

by BhariD (Sexton)
on Jun 19, 2011 at 13:16 UTC ( [id://910414]=perlquestion: print w/replies, xml ) Need Help??

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

I have the following code to find the occurrence of first longest substring of 1's

my $string = '00111100110'; $string =~ m/(1{1,}1)/; print $string, "\t", $1, "\n";

If I want to replace that longest substring ($1) found with a different number lets say '2' in the original $string, i.e. '00222200110' instead of '00111100110', how should I do that?

  • Comment on how to replace a matched pattern in a string with a different pattern of same length?
  • Download Code

Replies are listed 'Best First'.
Re: how to replace a matched pattern in a string with a different pattern of same length?
by BrowserUk (Patriarch) on Jun 19, 2011 at 13:21 UTC

    $s = '00111100110';; $s =~ s[(1+)]['2' x length $1]e;; print $s;; 00222200110

    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.
      Thanks!

      as I was testing the code I realize, the code that I am using is not matching the longest string of 1's

      my $string = '00110011111110111111111111111110'; $string =~ /(1{1,}1)/; print $1, "\n"; prints 11 instead of 11111111111111111

      I know to search for longest string separated by 1's, I would use =~ /(1.*1)/, but here "." will pick 0's and 1's both, and I only want 1's. I tried few other combinations but didn't seem to work. any suggestions?

        BharID:

        Look for all matches, select the longest one, and then perform your replacement. Something like (untested):

        my $string = '00110011111110111111111111111110'; my $str_to_replace; for ($string=~/(1+)/g) { my $temp = $1; $str_to_replace = $temp if length($tmp) > length($str_to_replace); } if ($str_to_replace) { my $repl = '2' x length($str_to_replace); $string =~ s/$str_to_replace/$repl/; }

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

        $s = '00110011111110111111111111111110';; my $best = [0,0];; $+[0] - $-[0] > $best->[1] and $best = [ $-[0], $+[0] - $-[0] ] while $s =~ m[(1+)]g;; substr $s, $best->[0], $best->[1], '2'x$best->[1];; print $s;; 00110011111110222222222222222220

        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.
Re: how to replace a matched pattern in a string with a different pattern of same length?
by rev_1318 (Chaplain) on Jun 19, 2011 at 18:38 UTC
    Quick and dirty:
    perl -le '$_="00110011111110111111111111111110"; $m=( sort({length($b) +<=>length($a)} /(1+)/g ) )[0];s/$m/2 x length $m/e;print'

    Paul

Re: how to replace a matched pattern in a string with a different pattern of same length?
by 7stud (Deacon) on Jun 20, 2011 at 09:37 UTC
    I know to search for longest string separated by 1's, I would use =~ /(1.*1)/, but here "." will pick 0's and 1's both, and I only want 1's. I tried few other combinations but didn't seem to work. any suggestions?

    1* is equivalent to 1{0,}
    1+ is equivalent to 1{1,}

    And 1{1,}1 is just nonsense. It matches a minimum of two 1's, but certainly does not match the longest run of 1's in a string--it just matches the *first* run of 1's that has at least two 1's in it. For instance,

    use strict; use warnings; use 5.010; my $str = '110011111111'; $str =~ s/1{1,}1/*/; say $str; --output:-- *0011111111
Re: how to replace a matched pattern in a string with a different pattern of same length?
by wind (Priest) on Jun 20, 2011 at 22:18 UTC
    First calculate the longest using List::Util->max and then do a substitution.
    use List::Util qw(max); use strict; use warnings; my $string = '00111100110'; my $longest = max map {length} $string =~ /(1+)/g; $string =~ s/1{$longest}/'2' x $longest/eg; print $string, "\n";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-25 17:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found