Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Perl-Sensitive Sunglasses
 
PerlMonks  

Creating a fill-in regexp

by jpfarmer (Pilgrim)
on Sep 24, 2003 at 14:33 UTC ( [id://293964]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

I'm working on a program where I'm dealing with long strings that look like this (only much longer): 0000010000000100000100001. I want to fill in the area between the 1s with zeros, so that string would become 0000011111111100000111111. I know that I can match the zeros with m/(?<=1)0+(?=1)/, but I don't know how to go about replacing all of the 0s with 1s just inside the match. Can I chain that with tr// somehow?

Replies are listed 'Best First'.
Re: Creating a fill-in regexp
by shenme (Priest) on Sep 24, 2003 at 14:50 UTC
    Use a 'substitute' ;-)
    $s1 = '0000010000000100000100001'; $s1 =~ s/1(0+)1/'1'x((length($1)+2))/eg;
    Output
     0000011111111100000111111
    
      It never occured to me that I could replace with a variable-length string. Oops. I feel pretty dumb now.

      Thanks for your help!
Re: Creating a fill-in regexp
by CombatSquirrel (Hermit) on Sep 24, 2003 at 14:51 UTC
    Use the s/// substitution operator:
    $string =~ s/(?<=1)(0+)(?=1)/'1' x length($1)/eg;
    Hope this helped.
    CombatSquirrel.
    Entropy is the tendency of everything going to hell.
Re: Creating a fill-in regexp
by Abigail-II (Bishop) on Sep 24, 2003 at 14:53 UTC
    Your description doesn't match your example. You say you want to fill the area between the 1's with zero's, but in your example, there are three area's between 1's, and out of the three, you replace two of them with 1's.

    If your question is, "how do I replace the 0's with 1's", use tr:

    tr/0/1/

    Abigail

Re: Creating a fill-in regexp
by Enlil (Parson) on Sep 24, 2003 at 14:55 UTC
    Here is one solution:
    s/(10*1)/1 x length($1)/eg;
    Note that you might want to change the * to a + depending on what you want to happen should the ones be right next to each other. updatewording was "change the * to a star", thanks L~R

    -enlil

Re: Creating a fill-in regexp
by Not_a_Number (Prior) on Sep 24, 2003 at 15:34 UTC

    As a sort of follow-up to Abigail-II's comment, note that for the string:

    101110101

    the three snippets posted so far give three different 'solutions' :-)

    dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://293964]
Approved by HyperZonk
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.