Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #109254 > unrolled thread
| Started by | Ganesh Pal <ganesh1pal@gmail.com> |
|---|---|
| First post | 2016-05-30 14:33 +0530 |
| Last post | 2016-05-30 14:33 +0530 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: re.search - Pattern matching review Ganesh Pal <ganesh1pal@gmail.com> - 2016-05-30 14:33 +0530
| From | Ganesh Pal <ganesh1pal@gmail.com> |
|---|---|
| Date | 2016-05-30 14:33 +0530 |
| Subject | Re: re.search - Pattern matching review |
| Message-ID | <mailman.37.1464599026.1839.python-list@python.org> |
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 top | Article view | comp.lang.python
csiph-web