Complex Regex Perl

Hi the below perl snippet will replace any three letter string in the beginning with a two letter string which is specified..but if i want to modfiy only certain characters for eg..

ABC - AB
CAB - AB
AAA - No Modifcations
1AB - AB
AB8 - AB

Whatever coming before or after of AB only have to be deleted whereas the strings may be left as it is..

Perl Snippet

use warnings;
use strict;
 
while (<DATA>) {
    chomp;
    s/ ^ [0-9a-z]{3} $ /hi/xi;
    print "$_\n";
}
 

Thanks Guys!!!!!

Just for additional info guys how to specify spaces in the regex for eg i need to change AB B to AB how the regex to be modified .Thanks for the help guys!!!!!!

Try:

while (<DATA>) {
    chomp;
    s/.AB/AB/ || s/AB./AB/;
    print "$_\n";
}
perl -pe 's/.?AB.?/AB/' file