In [1]: import re In [2]: a=re.compile(r'^([0-9]+).*') In [3]: matches=a.match('123123jkjkfdjsfkj324234234') In [4]: matches.groups() Out[4]: ('123123',)