Code show as below, but there is a problem: “255.255.255.256” will be processed into “255.255.255.25”
JavaScript
x
17
17
1
import re
2
3
ip_pattern = re.compile(r"((25[0-5]|2[0-4]d|[01]?dd?).){3}(25[0-5]|2[0-4]d|[01]?dd?)")
4
5
def get_ip_by_regex(ip_str):
6
"""Match IP from the given text and return
7
8
:param ip_str: like "255.255.255.255,255.255.255.256,260.255.255.255"
9
:type ip_str: string
10
:return: IP LIST ["255.255.255.255"]
11
:rtype: list[string]
12
"""
13
ret = []
14
for match in ip_pattern.finditer(ip_str):
15
ret.append(match.group())
16
return ret
17
If I pass the 255.255.255.255,255.255.255.256,260.255.255.255
string I expect ["255.255.255.255"]
as the results.
Advertisement
Answer
You want to implement comma boundaries, (?<![^,])
and (?![^,])
:
JavaScript
1
2
1
ip_pattern = re.compile(r"(?<![^,])(?:(?:25[0-5]|2[0-4]d|[01]?dd?).){3}(?:25[0-5]|2[0-4]d|[01]?dd?)(?![^,])")
2
See the regex demo.
Details
(?<![^,])
– a negative lookbehind that matches a location not immediately preceded with a char other than a comma (i.e. there must be a comma or start of string immediately to the left of the current location)(?![^,])
– a negative lookahead that matches a location not immediately followed with a char other than a comma (i.e. there must be a comma or end of string immediately to the right of the current location).
See the Python demo:
JavaScript
1
17
17
1
import re
2
3
ip_pattern = re.compile(r"(?<![^,])(?:(?:25[0-5]|2[0-4]d|[01]?dd?).){3}(?:25[0-5]|2[0-4]d|[01]?dd?)(?![^,])")
4
5
def get_ip_by_regex(ip_str):
6
"""Match IP from the given text and return
7
8
:param ip_str: like "255.255.255.255,255.255.255.256,260.255.255.255"
9
:type ip_str: string
10
:return: IP LIST ["255.255.255.255"]
11
:rtype: list[string]
12
"""
13
return ip_pattern.findall(ip_str)
14
15
print(get_ip_by_regex('255.255.255.255,255.255.255.256,260.255.255.255'))
16
# => ['255.255.255.255']
17