Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.182 X-Spam-Level: * X-Spam-Evidence: '*H*': 0.64; '*S*': 0.00; 'subject:help': 0.08; 'string': 0.09; '(data': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'trying': 0.19; 'skip:p 40': 0.19; 'split': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'looks': 0.24; 'header:In-Reply- To:1': 0.27; 'testing': 0.29; 'am,': 0.29; "doesn't": 0.30; "skip:' 10": 0.31; 'extract': 0.31; 'front': 0.32; 'text': 0.33; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'thanks': 0.36; 'message-id:@gmail.com': 0.38; 'depends': 0.38; 'handle': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'address.': 0.39; 'address': 0.63; 'email addr:gmail.com': 0.63; 'high': 0.63; 'provide': 0.64; 'chance': 0.65; 'charset:windows-1252': 0.65; 'skip:r 40': 0.68; 'acts': 0.74; 'address,': 0.75; 'yours': 0.88; 'capture': 0.91; 'lucky': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=VSCQBM8YNyBM9ih1yFMlk9fRwBa3ISCevnEBgLYisYo=; b=h25ZYIROFhlhDTt/ZC05O5aTMMywghqE0gljvcWp7jeL3oJFTxXvIxKhV2rdZacd8c zhGoYuXHKn6dJPgLhDkeS9p6W/IWT0/83wS9qieerI96Xci7RCMwift25HmKjuqkk2/S bOIffjDQ/bfzsm4x/Ne8uq5CCsxY9avhu30mEWR++3/EQZ8E3JmLaz1/+jdsiQx+3zj5 fwNQj5Jwnu8ui+poPPuxQAshcoYe1Vfdoau1nFlXWJxSCDW4cA8TgJjxJ166RwqttreT zddxn6XhCIB9T/WOCQ9Br2udIDKuI93tfyHwadA4ECUhSO4Ae7wlsX4K198XBxvqz4Xn UBrg== X-Received: by 10.180.104.228 with SMTP id gh4mr4508007wib.63.1423056905241; Wed, 04 Feb 2015 05:35:05 -0800 (PST) Date: Wed, 04 Feb 2015 14:35:02 +0100 From: Jugurtha Hadjar User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: re.findall help References: <11aa8a80-bfdc-4e6f-a922-cb64c4da0466@googlegroups.com> In-Reply-To: <11aa8a80-bfdc-4e6f-a922-cb64c4da0466@googlegroups.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1423056907 news.xs4all.nl 2891 [2001:888:2000:d::a6]:47625 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:85211 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,