You can handle both leading, trailing, and intervening stretches less than 5 long with the following regular expression substitution:
s/(^|[^A])(A{1,4})([^A]|$)/\1\L\2\E\3/g
There is one problem, though. In case of the sequence NANA
only the first A
will be replaced. The reason for this is that the second N
is consumed by the third parenthesis in the regular expression, for which reason it cannot be matched by [^A]
in the first parenthesis to produce a second match. The only solution that I can think of is to apply the regular expression twice, which is sufficient to ensure that all matches will be masked. In this case one has to remember to make sure that [^A]
does not match the a
produced by the first round of matching, which is done with a trivial modification:
s/(^|[^Aa])(A{1,4})([^Aa]|$)/\1\L\2\E\3/g
Applying this regular expression twice should do the job. It is a bit of a hack, I admit.
Edit: An alternative solution is to first mask all A-stretches and then subsequently unmask the ones that are 5 or longer:
s/A/a/g;
s/(a{5,})/\U$1\E/;
This works, except for the case of trailing As. See below.
This will also not work with leading As or for cases like NANANAN where only the first and the third instance will be replaced. See below.