>>> import re >>> x = re.compile(r'gcc\[(\d+)\]') >>> str = "gcc[12]" >>> x.findall(str) ['12']