Bash has built-in facilities to trim characters at the beginning and end of the value of a variable:

$ myvar="AAAthis is a testAAA"
$ echo ${myvar##*(A)}
this is a testAAA
$ echo ${myvar%%*(A)}
AAAthis is a test