Mailing List Archive

re.findall help
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
--
https://mail.python.org/mailman/listinfo/python-list
Re: re.findall help [ In reply to ]
On 03Feb2015 18:52, w3tmb1@gmail.com <w3tmb1@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

I would not be using re.findall.

If you have strings such as the one you describe I would:

- call .split() on the string to break it up on whitespace

- select the strings starting with "ip="

- split those strings on "=" and grab the stuff after the "="

- if you want to validate the address: split it on '.', check that there are 4 components, call "int()" on each component to check that is an int, and then check the value of the int as being in the range 0..255 inclusive

Write some code doing the above, test it, and if stuck, return with:

- the code

- example input for which it does not work

- an explaination of what it does do (including an output transcript showing the badness if possible)

- an explaination of how the output is wrong, and what it should look like

Cheers,
Cameron Simpson <cs@zip.com.au>
--
https://mail.python.org/mailman/listinfo/python-list
Re: re.findall help [ In reply to ]
On 02/04/2015 03:52 AM, w3tmb1@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,
--
https://mail.python.org/mailman/listinfo/python-list