re.findall help

Cameron Simpson cs at zip.com.au
Tue Feb 3 22:07:20 EST 2015


On 03Feb2015 18:52, w3tmb1 at gmail.com <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

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 at zip.com.au>



More information about the Python-list mailing list