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 ( Apologies re sending) Date: Sat, 28 May 2016 23:48:16 +0530 Lines: 49 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de ySZAQWTMQs8JetzO/9bV5wZqDuG7AYw0RtnIzKrzN4qA== 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; 'subject:sending': 0.16; 'try:': 0.18; 'to:name:python-list@python.org': 0.20; 'trying': 0.22; 'code.': 0.23; '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; 'could': 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: ( ': 0.84; '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=E/XDkyewgWyjta0pZYYZKHRHvchxJ8ioCiYvuQ3bm4E=; b=nb37glfbJTYK9ZHASkAE6wl18x646mBNb6Z7ouJ9QrngsRNuiSXRpueWbbQ2G9JpOO yk7LvSNIRndhP9y/Opni0I5H0jDKbA0st7ZMp4ZS97IhccG1fVvojo5jA+32DblYZwQ0 bBOw6YcbFNxtXaxds23IvKMv3tHz3gkm5OgQSmat4TjCdhjvhH1L/f0O6+JHArpCl8BU 0BhcLLPgvY2rd/l4AgBOcgqafNeA5iVjHfCWSzfOHNzB6NEhlpEBfSt0CUDOPtLgA7OD JgycQv4iAqW1mI8tTY1eG4fre6nuht6VM0OxxwjMJ8cyrleZ34UZYRe5GuRwQrTzfYYA Tpdg== 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=E/XDkyewgWyjta0pZYYZKHRHvchxJ8ioCiYvuQ3bm4E=; b=VIMpzgpfVcbAJ/RITm7fPYZ1aSQKwZrpqkkMPXiEU0EcC3RoMj593pCerTZAgDbKZC UUfSnv6Dz9ba/XMV/j31ACVYsfj5T00QeuCYse23cWwYBEwofOHMjFTAXIUWf4lOdeWL WdXu4+runnRWUintLV/aIeaudGNd5uHNzYrKf9oto1eG/1IW6jkch1wc4ThCNFRg+nJ4 4H2pldVTvHYHczpe2+fzVqNSYxUhRYgmGiaQzTFTTGBJE318pbg9A/FS3vBmSHzuWJkD v9Lc1vg95u9gKaGkw4LAeDK1kcsP+b4JQZU5dpA5j6jj/f4YVZSiRmEzhM+K68MFhZDo uWbw== X-Gm-Message-State: ALyK8tKwfbWGis5CX6mCm9KObTSVOWNs/5iGMvdC8v7gG/HgcKsjfZZq97oIS6md8VfmNvqsutRBlTMRj69fQQ== X-Received: by 10.31.171.69 with SMTP id u66mr11852872vke.119.1464459496086; Sat, 28 May 2016 11:18:16 -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:109220 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. 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)), ) 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