ThatOnePythonNoob ThatOnePythonNoob - 2 months ago 11
Python Question

Using regex to match a specific pattern in Python

I am trying to create regex that matches the following pattern:

Note :

x
is a number e.g. 2

Pattern:

u'id': u'x' # x = Any Number e.g: u'id': u'2'


So far I have tried the folllowing:

Regex = re.findall(r"'(u'id':u\d)'", Data)


However, no matches are being found.

Answer

You have misplaced single quotes and you should use \d+ instead of just \d:

>>> s = "u'id': u'2'"
>>> re.findall(r"u'id'\s*:\s*u'\d+'", s)
["u'id': u'2'"]
Comments