ThatOnePythonNoob ThatOnePythonNoob - 6 months ago 25
Python Question

Using regex to match a specific pattern in Python

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

Note :

is a number e.g. 2


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.


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'"]