In this particular case, there is no need to use regular expressions, because the search string is always "PY" and, as expected, will be at the beginning of the string, so string.find can be used for this task. The find function returns the position at which the substring in the given line or line is located, therefore, if it is found at the beginning of the line, the return value is 0 (-1, if not found at all), that is :: /// p>
In [12]: 'PY 2015'.find('PY') Out[12]: 0 In [13]: ' PY 2015'.find('PY') Out[13]: 1
Perhaps it would be nice to break the white spaces, i.e.:
In [14]: ' PY 2015'.find('PY') Out[14]: 2 In [15]: ' PY 2015'.strip().find('PY') Out[15]: 0
And further, if only a year is of interest, it can be extracted using split, i.e.:
In [16]: ' PY 2015'.strip().split()[1] Out[16]: '2015'
source share