Meysam Meysam - 1 year ago 64
Python Question

Why is this regex not matched in python

I need to find "

" in "
" with the following regex:


But the following code does not match anything:

rating= "7.1/10&nb"
p = re.compile(re.escape("\d{1}\.?\d{0,2}\/10"))
m = p.match(rating)
if m:
print("rating: {}".format(
print("no match found in {}".format(rating))

What's the problem with my code?

Answer Source

Change only one line in your code.

p = re.compile(re.escape("\d{1}\.?\d{0,2}\/10")) to re.compile(r"\d{1}\.?\d{0,2}\/10")

It will work smoothly.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download