Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: How can I expand my substring?

by Corion (Patriarch)
on Jun 30, 2014 at 12:02 UTC ( [id://1091699]=note: print w/replies, xml ) Need Help??


in reply to Re^2: How can I expand my substring?
in thread How can I expand my substring?

The "1" has significance, in the sense that it is the loop body. You could rewrite my code as:

while( $string1 =~ s/B-/BB/ or $string1 =~ s/-B/BB/ ) { # Replacement already happened in the while condition };

There are other ways, but I find this approach to be closest to your description.

Replies are listed 'Best First'.
Re^4: How can I expand my substring?
by Anonymous Monk on Jun 30, 2014 at 12:10 UTC
    Oups, I think it misses case 4 or not? I mean, if the string starts with --- and doesn't have any NNNN before the BBB, then the BBB should be expanded only to the right (if there is space).

      Ah, I misread that part of the specification.

      Most likely, the easiest approach is now to do replacement with /e and look-ahead and look-behind:

      # Expand string to the left $string4 =~ s!(?<=N)(-+)(?=B)!'B' x length $1!e; # Expand string to the right $string4 =~ s!(?<=B)(-+)!'B' x length $1!e;
        Now it works for all cases...
        If I am not overdoing it, could you please explain your syntax a bit? I am new to Perl and doesn't seem to grasp your substitution way here...
        Hi there!
        I was wondering, how can we modify the regexp so as the "expansion" of the BBB label is done for a maximum of 200 positions to the left and to the right (and not till we reach the N label)?
Re^4: How can I expand my substring?
by Anonymous Monk on Jun 30, 2014 at 12:07 UTC
    Thanks Corion! Works smoothly!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-26 03:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found