TAGS :Viewed: 11 - Published at: a few seconds ago

[ Regex not working with python ]

Hey guys I am new to python regex. Since I am bad in regex i never spend more time in regex but i have a project now and i face some problems in my project.

When I run following piece of code

import re
line = "100, 1000 or 10000?";
num = re.sub(r'{3,4}', "", line)
print num

I get error like

File "/usr/local/lib/python2.7/re.py", line 242, in _compile
raise error, v # invalid expression
sre_constants.error: nothing to repeat

Since am new to regex i don't know why am getting this. Hope you guys can help me.

Answer 1


{3,4} is a modifier; there needs to be something to modify. It basically means "from three to four of". What does "from three to four of" even mean?

\d is "a digit". \d{3,4} means "from three to four of a digit" (i.e. 3-4 digits).