I am trying to create regex that matches the following pattern:
Note : x
is a number eg 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.
This regex will match your patterns:
u'id': u'(\\d+)'
The important bits of the regex here are:
()
which makes a capture group (so you can get the information \\d
which specifies any digit 0 - 9 +
which means "at least 1" Tested on the following patterns:
u'id': u'3'
u'id': u'20'
u'id': u'250'
u'id': u'6132838'
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'"]
Try this:
str1 = "u'id': u'x'"
re.findall(r'u\\'id\\': u\\'\\d+\\'',str1)
You need to escape single-quote(') because it's a special character
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.