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


Groups > comp.lang.python > #109221

RE: re.search - Pattern matching review ( Apologies re sending)

From Albert-Jan Roskam <sjeik_appie@hotmail.com>
Newsgroups comp.lang.python
Subject RE: re.search - Pattern matching review ( Apologies re sending)
Date 2016-05-28 19:12 +0000
Message-ID <mailman.15.1464462809.1839.python-list@python.org> (permalink)
References <CACT3xuXhA9U1Cwsw_OUW-Zfu96o-Bd9-OHFas80=M9etB0=bsw@mail.gmail.com> <DUB123-W24AEE854E04581EC90DDE283430@phx.gbl>

Show all headers | View raw


> Date: Sat, 28 May 2016 23:48:16 +0530
> Subject: re.search - Pattern matching review ( Apologies re sending)
> From: ganesh1pal@gmail.com
> To: python-list@python.org
> 
> Dear Python friends,
> 
> I am  on Python 2.7 and Linux . I am trying to extract the address
> "1,5,147456:8192" from the below stdout using re.search
> 
> (Pdb) stdout
> 'linux-host-machine-1: Block Address for 1,5,27320320:8192 (block
> 1,5,147456:8192) --\nlinux-host-machine-1: magic
> 0xdeaff2fe         mark_cookie 0x300000000000000a\n'
> (Pdb) type(stdout)
> <type 'str'>
> 
> Here is the code I have come up with, this looks buggy please review
> the same and suggest any better ways  to code.
> 
> Could we use splitlines() or re.complie() etc , my intention is to
> match 1,5,147456:8192 and return the same.
> 
> 
> #Sample code
> 
> import re
> import subprocess_run
> 
> def get_block():
>     try:
>         cmd = "get_block_info -l"
>         # stdout is the output retrieved by subprocess.Popen()
>         stdout, stderr, exitcode = subprocess_run(cmd)
>         search_pat = 'Block Address.* \(block (\d+),(\d+),(\d+):(\d+)'
>         matched = re.search(search_pat, stdout)
>         block = (int(matched.group(1)),
>                    int(matched.group(2)),
>                    int(matched.group(3)),
>                    int(matched.group(4)),
>                   )
Perhaps:map(int,  re.search(search_pat, stdout).groups())
Or re.findall
>     except IOError, e:
>         logging.warning('Error reading lines from "%s" (%s).'
>                 % (cmd, e))
> 
>     if block is None:
>        logging.error("block not found")
>        return False
>     logging.info("block not found")
>     return block
> 
> Regards,
> 
> Ganesh
> -- 
> https://mail.python.org/mailman/listinfo/python-list
 		 	   		  

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


Thread

RE: re.search - Pattern matching review ( Apologies re sending) Albert-Jan Roskam <sjeik_appie@hotmail.com> - 2016-05-28 19:12 +0000

csiph-web