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

$string1='--------NNNNNNN--------NNNNN--------NNNNN------NNNNNNN------ +-------BBBBB-----';

should become
$string1_new='--------NNNNNNN--------NNNNN--------NNNNN------NNNNNNNBB +BBBBBBBBBBBBBBBBBBBBB';

$string2='---------NNNNN-NNNNNNNNNNNNN----NNNNNNNNNNN---------BBBBBBBB +BB';
should become
$string2_new='---------NNNNN-NNNNNNNNNNNNN----NNNNNNNNNNNBBBBBBBBBBBBB +BBBBBB';

$string3='-------NNNNNNN-----------------BBBBBBBBBBBBBBB-------------N +NNNNNN';

should become
$string3_new='-------NNNNNNNBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB +BBBNNNNNNN';

$string4='--------BBBBBBBBBBBB-------NNNNNNNNNNNNN------NNNNNN';

should become:
$string4_new='--------BBBBBBBBBBBBBBBBBBBNNNNNNNNNNNNN------NNNNNN';

Does this make more sense? Thank you for the answer, I just don't get this "1" that you have there... I tried the:
$initial_protein=~s/B-/BB/g; $initial_protein=~s/-B/BB/g;

but didn't work...

Replies are listed 'Best First'.
Re^3: How can I expand my substring?
by Corion (Patriarch) on Jun 30, 2014 at 12:02 UTC

    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.

      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;
      Thanks Corion! Works smoothly!