I am just writing a small regex to filter email from string. When I am using pattern as patt = r'[\w.-]+@[\w.-]+'
, it's working fine. But when I am using pattern as patt1 = r'[\w-.]+@[\w-.]+'
, its giving me error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 146, in search
return _compile(pattern, flags).search(string)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 251, in _compile
raise error, v # invalid expression
sre_constants.error: bad character range
Code:
1st case:
>>> str = "hello@abc.com"
>>> patt = r'[\w.-]+@[\w.-]+'
>>> match = re.search(patt, str)
>>> match.group()
'hello@abc.com'
2nd case:
>>> str = "hello@abc.com"
>>> patt = r'[\w-.]+@[\w-.]+'
>>> match = re.search(patt, str)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 146, in search
return _compile(pattern, flags).search(string)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 251, in _compile
raise error, v # invalid expression
sre_constants.error: bad character range
Any idea what I am doing wrong in the second regex?