Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ganesh Pal Newsgroups: comp.lang.python Subject: re.search - Pattern matching review Date: Sat, 28 May 2016 23:42:15 +0530 Lines: 49 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de WUSZ9u3Q3PQAdPfs34koZQYYSgzmP2dDxqKPijPXfUqg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'none:': 0.05; 'retrieved': 0.05; '"%s"': 0.09; 'cmd': 0.09; 'matched': 0.09; 'stdout': 0.09; 'python': 0.10; '2.7': 0.13; 'output': 0.13; 'def': 0.13; 'suggest': 0.15; '(pdb)': 0.16; 'buggy': 0.16; 'e))': 0.16; 'ioerror,': 0.16; 'magic': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'try:': 0.18; 'to:name:python- list@python.org': 0.20; 'trying': 0.22; 'import': 0.24; 'linux': 0.26; 'message-id:@mail.gmail.com': 0.27; 'skip:( 20': 0.28; 'looks': 0.29; 'code': 0.30; 'extract': 0.33; "skip:' 20": 0.34; 'except': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'false': 0.35; 'friends,': 0.35; 'skip:i 20': 0.36; 'lines': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'skip:& 10': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'skip:- 20': 0.39; 'to:addr:python.org': 0.40; 'address': 0.61; 'here': 0.66; 'dear': 0.67; 'url:info': 0.71; 'subject:review': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to; bh=ifV9XA4404LzC0OJg5ogoWEbwsY8lzY+lWsaT6tanXo=; b=TMjHk47Km4vZeqlPp+I/xoH5tTujAMEpIajXIHO/eXEChqFVXd2DNOw3+kx+DV/2Wt 4YIWqiKViE/VJf6PmHztYGtaEKU09zbtqPW8yk3d0LT6kfAA3PU0j0n7PHA/rhWSB6qN m2yYKNoPFO2kz57YOCkSVd1uDHZVOGUn/x6LssyYcvneVikoo0N16arVMFsa2nOWdsB8 /eBUH3LSpPGZZdAB/KuRgHNnuYd73A2uoa/mjkQ4CxWrA0zQDz2+KfhYKnmROlx0ctB7 +R/p8e/1UERUoOYQsa+bwlShxqwelB9dlIYe9LJoMGvdJDJjxchuzNi7qUloxubyC4ST pU2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:date:message-id:subject:from:to; bh=ifV9XA4404LzC0OJg5ogoWEbwsY8lzY+lWsaT6tanXo=; b=I/nzKZnz2kIlUUBUQaLkNMIivzbCj5EaUvUV/pwQ+gvmnje3thKPUllVUBiZAvCVbI 35FH5cfdk5+D4U1Q2pkKOad+0nhfSKsxfmSQ+O0hXaWRDSDmUm8g09i8sFMXePDRKdyp gqciHrjTw0TwxnA5AE7A0KTvxaxsEiaDqe2lKG1FCyHiHiYeTg1uZY1hrHM+oMpHT99E Gm6D18HocAf+zqjpaVblnERkRSSDWHAn4msdBkGPU49ujLOiNZlIM2A9SgWWLoZlYEmw WVPK3B8dTYYCpbRuCgwmqs4ymoiLBclMA5JMpFWoZgteYFltNRFGhmn/oRPvElU8a9kN td8g== X-Gm-Message-State: ALyK8tIfOPsKaUKuVN33JD7qzK63bTy70fzkhLyWkh5Fj6/zEJB0+LeDK0HO2Rtu7FMZFrqfmetlkBl+X1Ghjw== X-Received: by 10.159.34.9 with SMTP id 9mr9700409uad.25.1464459135363; Sat, 28 May 2016 11:12:15 -0700 (PDT) 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: Xref: csiph.com comp.lang.python:109219 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) Here is the code I have come up with, this looks buggy please review the same and suggest any better ways to code the same the same may be splitlines() , re.complie() etc , my intention is to just 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)), ) 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