I am posting the solution that I am currently using. Thanks to @markp-fuso and @renaud-pacalet for the ideas input. I am able to solve it.
Python script
import re
text = "^[[200~a^[[200~aaa aM1bb bbbM1ccc[$cM2ddddM2eeeeeM3ffffff fM3ggggggg M3hhhhh hhM3kkkkk~"
# Extract the text between the first M1 and the last 'M3'
result = re.search(r'M1(.*)M3.*$', text).group(1)
# Extract the text between the last M1 and the first 'M3'
result = re.sub(r'.*M1|M3.*','',text)
sed way of extracting is
# Extract the text between the first M1 and the last 'M3'
echo "^[[200~a^[[200~aaa aM1bb bbbM1ccc[\$cM2ddddM2eeeeeM3ffffff f:M3ggggggg M3:hhhhh hhM3:kkkkk~" | sed -E "s/M1(.*)M3.*$/\n\1/;s/.*\n(.*)/\1/"
# Extract the text between the last M1 and the first 'M3'
echo "^[[200~a^[[200~aaa aM1bb bbbM1ccc[\$cM2ddddM2eeeeeM3ffffff f:M3ggggggg M3:hhhhh hhM3:kkkkk~" | sed -E "s/.*M1|M3.*//g"