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!