re.findall help
Jugurtha Hadjar
jugurtha.hadjar at gmail.com
Wed Feb 4 08:35:02 EST 2015
On 02/04/2015 03:52 AM, w3tmb1 at gmail.com wrote:
> I am trying to extract the following from a data stream using find
> all what would be the best way to capture the ip address only from
> the following text " ip=192.168.1.36 port=4992 " I also want to make
> sure the program can handle the ip that is as high as
> 255.255.255.255
>
> Thanks for any help you can provide
>
Hello,
It depends on whether you trust the data (if it's yours and you *know*
they're IP addresses) and just want to automate, or not..
>>> pattern = re.compile(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
Tested on:
>>> test = "ip=192.168.1.36 port=4992 ip=255.255.255.255 port=80"
Gives:
['192.168.1.36', '255.255.255.255']
Add "ip=" in order to avoid confusion (data may have by chance something
that looks like an IP address, but the "ip=" bit acts as a discriminant.
>>> pattern = re.compile(r'ip=\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
Testing for the string test:
test = "ip=192.168.1.36 port=4992fdjsqklmfqsjdkip=192.168.541.36
port=222 2.2.2.2random"
Gives:
['ip=192.168.1.36', 'ip=192.168.541.36']
It ignores the 2.2.2.2 because it doesn't have "ip=" in front of it, and
was just lucky to have an IP address structure.
You can then split "ip=" out of each item to have the IP address.
--
~Jugurtha Hadjar,
More information about the Python-list
mailing list