if I have a list ['default via 192.168.0.1 dev wlan0 proto static \n', '192.168.0.0/24 dev wlan0 proto kernel scope link src 192.168.0.11 \n'] what will be the most pythonic way to extract 192.168.0.1 and 192.168.0.11 out of it
2 Answers
Try this using regular expressions, where lst is the list with the data:
import re
pat = re.compile(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}[^/]')
[pat.search(s).group(0) for s in lst]
=> ['192.168.0.1', '192.168.0.11']
The above assumes that there's a valid IPv4 IP in each string in the list, and that after the IP there isn't a / character.
2 Comments
OmnipotentEntity
This code doesn't give OP the answer he was looking for. "to extract 192.168.0.1 and 192.168.0.11 out of it"
Óscar López
@OmnipotentEntity there you go!