Reputation: 149
I want to check if a string contains some part that matches a given regex pattern. My regex is to check for the presence of an IP address, it goes like this
regex = '''^(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]?)$'''
I want to check if a string like this contains an IP address in it
url_string = "http://110.234.52.124/paypal.ca/index.html"
since it has an IP address, I want to detect that, how can I do that?
Upvotes: -3
Views: 469
Reputation: 182
import re
regex = "(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]?)"
result = re.search(regex, "http://110.234.52.124/paypal.com")
you just need remove ^ and $ and call this function if result is None that means not found
Upvotes: 1
Reputation: 627335
There are at least two issues with the regex:
re.X
or re.VERBOSE
options are required for it to work^
and $
anchors here that require a full string match. You probably want to use word boundaries, \b
insteadr
prefix and make it a raw string literal, you can just use \b
(?<!\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).
Upvotes: 1