There are at least two issues with the regex:
- It contains whitespace that is used as formatting whitespace, a
re.X
or re.VERBOSE
options are required for it to work
- There are
^
and $
anchors here that require a full string match. You probably want to use word boundaries, \b
instead
- If you add word boundaries, the regular string literal will require doubling backslashes, but if you add a
r
prefix and make it a raw string literal, you can just use \b
- If there are other dot-separated number strings that are not IPs and need filtering out, you need
(?<!\d)(?<!\d\.)
at the start instead of ^
and (?!\.?\d)
at the end instead of $
.
You can use
import re
regex = r'''\b(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(
25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(
25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(
25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\b'''
url_string = "http://110.234.52.124/paypal.ca/index.html"
print( bool(re.search(regex, url_string, re.X)) )
# => True
See the Python demo
However, you may define an octet pattern as a variable, and build a pattern dynamically removing the need to use re.X
and that long pattern:
import re
o = r'(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)'
regex = fr'\b{o}(?:\.{o}){{3}}\b'
# OR regex = fr'(?<!\d)(?<!\d\.){o}(?:\.{o}){{3}}(?!\.?\d)'
url_string = "http://110.234.52.124/paypal.ca/index.html"
print( bool(re.search(regex, url_string, re.X)) )
# => True
See the Python demo. Note the double braces around the {{3}}
(in an f-string, literal braces are defined with double braces).