Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #85211

Re: re.findall help

Date 2015-02-04 14:35 +0100
From Jugurtha Hadjar <jugurtha.hadjar@gmail.com>
Subject Re: re.findall help
References <11aa8a80-bfdc-4e6f-a922-cb64c4da0466@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.18460.1423056907.18130.python-list@python.org> (permalink)

Show all headers | View raw


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,

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

re.findall help w3tmb1@gmail.com - 2015-02-03 18:52 -0800
  Re: re.findall help Cameron Simpson <cs@zip.com.au> - 2015-02-04 14:07 +1100
  Re: re.findall help Jugurtha Hadjar <jugurtha.hadjar@gmail.com> - 2015-02-04 14:35 +0100

csiph-web