ThatOnePythonNoob ThatOnePythonNoob - 1 year ago 63
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.

Answer Source

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'"]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download