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


Groups > comp.lang.python > #109254

Re: re.search - Pattern matching review

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ganesh Pal <ganesh1pal@gmail.com>
Newsgroups comp.lang.python
Subject Re: re.search - Pattern matching review
Date Mon, 30 May 2016 14:33:43 +0530
Lines 53
Message-ID <mailman.37.1464599026.1839.python-list@python.org> (permalink)
References <CACT3xuVzgYxA_Yb4LngimMpp1zhBAz5qwY1o0qcoYZ+WMtUdAQ@mail.gmail.com> <CAG93HwHRh2F9dcMoZarZkradj659hyccaoMcCF9gWHc2zioaAQ@mail.gmail.com> <CACT3xuX_e75R3M2LkPH9qvFzNzY-TJfD2g2VJAgkMteiwu=qkA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de uwYTY6qtE2Peu1MSL7YJTAgLmjp63EbB3EKJWVcKC0Og==
Return-Path <ganesh1pal@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'error:': 0.05; 'cc:addr:python-list': 0.09; 'cmd': 0.09; 'complicate': 0.09; 'matched': 0.09; 'stdout': 0.09; 'tuple': 0.09; 'exception': 0.13; 'question.': 0.13; 'def': 0.13; '2016': 0.16; 'determining': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sfxlen:0': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'trying': 0.22; 'header:In-Reply- To:1': 0.24; "doesn't": 0.26; 'example': 0.26; 'message- id:@mail.gmail.com': 0.27; 'idea': 0.28; 'looks': 0.29; 'attempting': 0.29; 'code': 0.30; 'task': 0.30; 'included': 0.32; 'except': 0.34; 'add': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'could': 0.35; 'i.e.': 0.35; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'skip:& 10': 0.37; 'thanks': 0.37; 'received:209.85.213': 0.37; 'seem': 0.37; 'received:209': 0.38; 'your': 0.60; 'here': 0.66; 'below.': 0.66; 'reply': 0.68; 'url:info': 0.71; 'analysis': 0.72; '10:32': 0.84; 'construct': 0.84; 'subject:review': 0.84; '\xc2\xa0and': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc; bh=a2xF4iRIynJEEV9IZl7i+QzNlLRZHVXlVkcifrLvQWo=; b=sYa+5cGvq2A3Tm3s48zd6/kjGLUaPA2noqov+K24ouqsJwS2h2yqDonm2FLomhMjOA 7GXVCcSs0pTgDlO5oV+XQKZjNliBmOPWeEDCtDL5pIxAqS8M3TQNwxYIM7xnc+2L2qOH idiqFwD1CbLpuLxfmity0b5duAqBsJiuG+nqnNEso627fSIsBqUUOiYafOuYHUN2kPpW lYxM6ZuLKEdto+sp77vQ6VCuQAU++EaAnC2TjvgRwO3DnvwxXUpCX9YH9j3xWc2KFvuD e4T/71sTKs0LFrSd03/cr9ljHHWgzDju4WjOBqQcrFaL1iaABOoDUQ17cbj4XxHsQBCu 0Qow==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc; bh=a2xF4iRIynJEEV9IZl7i+QzNlLRZHVXlVkcifrLvQWo=; b=cSEHhxMCbk9UXluI7Fn9HXIFP7C3O0mDXXrwaR/W5+JFeLCh1cDAWxQBddtJoQhKOM U8FcGf9x9hzOPIbbeD3GPaNTaQIrTQtDaDOe2LTkXcV0DY3t4plXxtJMuF9AKcFxVOTY 3D8npLQYkLfzqIPtWQ+LJcW9lrbNBYb90zHa5kx3TWTZcuudadXiBQVSKFpG8tR1i1n2 kJ7V2/5Gkg34eeSzWiSeApo8fBU//cMzaGJQGn8VglrqlXfizwHGyICv4vYwfcRsocex Zi+DsTPcc+Qxm6rwc8y7u6b2DvvF6seA1S2zM3WHf8XUHa5kj0RGHprCT1wc2RrHbKB7 lR7A==
X-Gm-Message-State ALyK8tLGe8gdnxusuTYMpjz0Qor5n/LDKUEOv+aRZ0jf+larC1JMZhip+dJ6WdSkTt08qKOTj1RdhNHDTOTQrA==
X-Received by 10.31.6.149 with SMTP id 143mr14312524vkg.33.1464599023825; Mon, 30 May 2016 02:03:43 -0700 (PDT)
In-Reply-To <CAG93HwHRh2F9dcMoZarZkradj659hyccaoMcCF9gWHc2zioaAQ@mail.gmail.com>
X-Content-Filtered-By Mailman/MimeDel 2.1.22
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <CACT3xuX_e75R3M2LkPH9qvFzNzY-TJfD2g2VJAgkMteiwu=qkA@mail.gmail.com>
X-Mailman-Original-References <CACT3xuVzgYxA_Yb4LngimMpp1zhBAz5qwY1o0qcoYZ+WMtUdAQ@mail.gmail.com> <CAG93HwHRh2F9dcMoZarZkradj659hyccaoMcCF9gWHc2zioaAQ@mail.gmail.com>
Xref csiph.com comp.lang.python:109254

Show key headers only | View raw


On Sun, May 29, 2016 at 10:32 PM, Matt Wheeler <m@funkyhat.org> wrote:

>
>
> This doesn't seem to exactly match your code below, i.e. your code is
> attempting to construct a tuple from groups 1 through 4. To meet this
> specification I could just `return re.search('(?<=\(block
> )[^(]*(?=\))', stdout).group()`
>
> Thanks Matt for the reply  and lovely analysis . I was trying to
complicate the simple task :(

Here is how the code looks now , the whole idea was just to  match the
pattern and return it


def get_block(block):

    cmd = "get_block_info -l"
    stdout, stderr, exitcode = subprocess_run(cmd)
    #Grab the block from the stdout
    block = re.search('(?<=\(block )[^(]*(?=\))', stdout).group()
    # check the pattern
    matched = re.search(r'(\d+),(\d+),(\d+):(\d+)', block)
    if matched:
       logging.info('block found")
       return block
    else:
       logging.info('block not found")


I had one final question. I was thinking if we included a try -expect
block to catch the failures of re.search  as shown below.

what kind of specific exception can we add ( just the  AttributeError
Exception or any thing else )


Example :

try:

    block = re.search('(?<=\(block )[^(]*(?=\))', stdout).group()

    matched = re.search(r'(\d+),(\d+),(\d+):(\d+)', block)

except AttributeError

     logging.error(' Error: while determining the block ")

Regards,

Ganesh

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


Thread

Re: re.search - Pattern matching review Ganesh Pal <ganesh1pal@gmail.com> - 2016-05-30 14:33 +0530

csiph-web