Q:

Stripping start/end characters on regular expression

I have the following regular expression:
>>> re.findall(r'\r\n\d+\r\n',contents)[-1]
'\r\n1621\r\n'
>>> re.findall(r'\r\n\d+\r\n',contents)[-1].replace('\r','').replace('\n','')
'1621'

How would I improve the regular expression such that I don't need to use the python replace methods?
Note that the digit must be surrounded by those characters, I can't do a straight \d+.

A:

Simply use parenthesis:
re.findall(r'\r\n(\d+)\r\n',contents)[-1]

That way you match the given pattern and only get the parenthesis content in findall result.

