in reply to How can I expand my substring?

In your example, what is your expected result?

Also, do the strings need to be as long as you've shown? This makes reading your example quite unwieldly. Maybe shortening each group of characters to four characters helps make the example data more understandable?

Personally, I would solve the problem exactly as you described. Replace any - followed by a B by BB, and the same for the other direction. Keep doing that as long as you can:

$string1='--NN--NNN--NN--NNN--BBB-----'; 1 while $string1 =~ s/B-/BB/ or $string1 =~ s/-B/BB/; print $string1;

Replies are listed 'Best First'.
Re^2: How can I expand my substring?
by Anonymous Monk on Jun 30, 2014 at 12:00 UTC
    $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...

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