Description
Given a string consisting of only A's and B's,
and we divide that string from left to right into units.
We define 'unit' as:
| (a) | 1-char-unit: 'B'. |
| (b) | 2-char-unit: either 'AB' or 'AA' |
Now we want to replace every 'B' in 1-char-units with 'C', but
retain those in 2-char-units.
|
| Raw Input
| BBABBAAABBABAABAAABB
|
|
| Desired Output
| CCABCAAABCABAACAAABC
|
|
Script and Comments
Script1 [sed] [ 1] :loop
[ 2] s/^\(\(B*\(A[AB]\)*\)*\)B/\1C/
[ 3] t loop
|
Script2 [perl] [ 1] while(s/^((B|A[AB])*)B/$1C/) { };
[ 2] print;
| |