Regular Expression Perl

$str = "A and B";
$str =~ s/(A) and (B)/$2, $1/i;  
print $str;