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


Groups > comp.lang.python > #109254

Re: re.search - Pattern matching review

From Ganesh Pal <ganesh1pal@gmail.com>
Newsgroups comp.lang.python
Subject Re: re.search - Pattern matching review
Date 2016-05-30 14:33 +0530
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>

Show all headers | 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