For example, let's search for all possible alignments matches (M operator) in CIGAR:
In : import re
In : match = re.findall(r'(\d+)M', '40M25N5M')
In : print match
Expression \d+M represents all strings having pattern "nM", where n is a number that can consist from one or multiple digits. Round brackets create a group from the number, so it can be accessed later.
Similarly one can iterate over a CIGAR string:
In : match = re.findall(r'(\d+)(\w)', '40M25N5M')
In : match
Out: [('40', 'M'), ('25', 'N'), ('5', 'M')]
Here we use \w meta-symbol to represent any letter and round brackets for grouping.