Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Karim Newsgroups: comp.lang.python Subject: Re: Newbie: Check first two non-whitespace characters Date: Thu, 31 Dec 2015 19:54:23 +0100 Lines: 27 Message-ID: References: <240ab049-68a0-4a00-b911-d58cae9bfbcf@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de Pqx1KH2tFkAGnHu+8hjYvAfGU2H5dksTiTVKOVYzLOCQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.030 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'ideally': 0.04; 'subject:two': 0.07; 'subject:characters': 0.09; 'forward:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:non': 0.16; 'wrote:': 0.16; 'string': 0.17; '>>>': 0.20; 'to:2**1': 0.21; 'doc': 0.22; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header :User-Agent:1': 0.26; '(which': 0.26; 'to:no real name:2**1': 0.27; 'be:': 0.29; 'depth': 0.29; 'print': 0.30; 'message- id:@gmail.com': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'something': 0.35; 'received:74.125.82': 0.35; 'but': 0.36; 'should': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'list.': 0.37; 'received:192': 0.39; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'space': 0.40; 'your': 0.60; "you'll": 0.61; 'hope': 0.61; 'email addr:gmail.com': 0.62; 'charset:windows-1252': 0.62; 'received:74.125.82.41': 0.84; 'subject:Check': 0.95 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-type:content-transfer-encoding; bh=TcyQmxSmYt4S9UQh3SJEV+/nmR0TlRM1IIJyR7MHBto=; b=vVM0SiSedhrFhRFd61a1GlyyODMwTWhpFMhBNqfUziafMxeKsrWqdWeiY7fBc3jUJQ F2rUbyDheKYIo8gZlmjdBGjVzBEyMUV/ag2fYbvJ8olgAQ9hEukZSj+43iTEFsFCAwoM j+wuUk+n7jPbryrhnQ9/SmcxBPgPNaBE3cpC8yv1RgKw4fI8UgNh069By+pRYAS4eJDi 8FDV58HSX0kI9cyqV7085zAaGhN0IJy0ac6vAZ5TnYGqAi3UZjURR5TVYJKUko/8C6SR RTT9IpfgxapJ/nt4CxMOPs0bWvOSNEQWPUz0Sdt8qPaLZsY5n8xQrdl1W7onC6L1VIXM deHg== X-Received: by 10.28.195.138 with SMTP id t132mr56402772wmf.61.1451588064958; Thu, 31 Dec 2015 10:54:24 -0800 (PST) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 In-Reply-To: <240ab049-68a0-4a00-b911-d58cae9bfbcf@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:101072 On 31/12/2015 19:18, otaksoftspamtrap@gmail.com wrote: > I need to check a string over which I have no control for the first 2 non-white space characters (which should be '[{'). > > The string would ideally be: '[{...' but could also be something like > ' [ { ....'. > > Best to use re and how? Something else? Use pyparsing it is straight forward: >>> from pyparsing import Suppress, restOfLine >>> mystring = Suppress('[') + Suppress('{') + restOfLine >>> result = mystring.parse(' [ { .... I am learning pyparsing' ) >>> print result.asList() ['.... I am learning pyparsing'] You'll get your string inside the list. Hope this help see pyparsing doc for in depth study. Karim