Issue
I must be missing something simple - whatever I do, I can't get my regex to match any strings:
[~] $ python2.7
Python 2.7.12 (default, Aug 13 2016, 19:37:25)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> s = " 405489796130 "
>>> regex = "\b[0-9]{15}|[0-9]{12}\b"
>>> for str in re.findall(regex, s):
... print(str)
...
>>> for str in re.finditer(regex, s):
... print(str)
...
>>> print("Hi")
Hi
>>>
The regex "\b[0-9]{15}|[0-9]{12}\b"
should definitiely match the provided string (that string contains a substring of 12 digits...).
I even put this text and the regex into https://regexr.com/ and that website's regex found the substring - why can't Python?
Solution
You have to escape your back-slashes.
regex = "\\b[0-9]{15}|[0-9]{12}\\b"
Answered By - mypetlion
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.