Domanda

So I have a document (plain text) that I'm trying to extract all of the IP addresses from. I was able to extract them using regular expressions but it also grabs a large number of version numbers. I tried using string.find() but it requires that I be able to locate the escape character used for the end of the line (the IP addresses are always the last thing on a line) and the escape character used for the end of the line is unknown to me. Anyone know how I could pull these addresses out?

È stato utile?

Soluzione

If your addresses are always on the end of a line, then anchor on that:

ip_at_end = re.compile(r'(?:[0-9]{1,3}\.){3}[0-9]{1,3}$', re.MULTILINE)

This regular expression only matches dotted quads (4 sets of digits with dots in between) at the end of a line.

Demo:

>>> import re
>>> ip_at_end = re.compile(r'(?:[0-9]{1,3}\.){3}[0-9]{1,3}$', re.MULTILINE)
>>> example = '''\
... Only addresses on the end of a line match: 123.241.0.15
... Anything else doesn't: 124.76.67.3, even other addresses.
... Anything that is less than a dotted quad also fails, so 1.1.4
... does not match but 1.2.3.4
... will.
... '''
>>> ip_at_end.findall(example)
['123.241.0.15', '1.2.3.4']

Altri suggerimenti

Description

this will match and validate ipv4 addresses, and will ensure the individual octects are within a range of 0-255

(?:([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])

enter image description here

Disclaimer

yes I realize the OP asked for a Python solution. This PHP solution is only included to show how the expression works

php example

<?php
$sourcestring="this is a valid ip 12.34.56.78
this is not valid ip 12.34.567.89";
preg_match_all('/(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])/i',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>

$matches Array:
(
    [0] => Array
        (
            [0] => 12.34.56.7
        )

)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top