I would extract all the numbers after a symbol contained in a output. Which is the better suited for the purpose, regular expressions ?
for eg i have an output as 172.4.5.3/32
and i need to filter this output as /32
I would extract all the numbers after a symbol contained in a output. Which is the better suited for the purpose, regular expressions ?
for eg i have an output as 172.4.5.3/32
and i need to filter this output as /32
In this particular case, I'd go with Python's classes designed for dealing with IP addresses:
from ipaddress import IPv4Network
n = IPv4Network('172.4.5.3/32')
print(n.prefixlen)
This splits the string into multiple strings (split where /
occurs) and are put into an array. After this I select the last element of this array and prepend /
.
input = "172.4.5.3/32"
output = '/' + input.split('/')[-1]
print(output)
output:
/32
You can split it on '/', or use re.findall.
re.findall('/.*', '172.4.5.3/32')[0]
outputs '/32'
The python .split()
method allows you to split a string into multiple items of a list, splitting on a separator of your choice.
ip_string = '172.4.5.3/32'
ip_subnet = ip_string.split('/')[1]
will fetch you your second value of that split string, i.e. the '32'