Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: MRAB Newsgroups: comp.lang.python Subject: Re: Newbie: Check first two non-whitespace characters Date: Thu, 31 Dec 2015 18:38:01 +0000 Lines: 18 Message-ID: References: <240ab049-68a0-4a00-b911-d58cae9bfbcf@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de lwxn2iB91GS/L3XeM6rW6AUuYgOEmrNoum6OQlrDa5Bw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'ideally': 0.04; 'subject:two': 0.07; 'subject:characters': 0.09; '1))': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:non': 0.16; 'wrote:': 0.16; 'string': 0.17; '>>>': 0.20; 'header:In-Reply-To:1': 0.24; 'header :User-Agent:1': 0.26; '(which': 0.26; 'be:': 0.29; 'skip:. 10': 0.32; 'could': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'faster': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:192': 0.39; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'space': 0.40; 'maximum': 0.61; 'provide': 0.61; 'email addr:gmail.com': 0.62; 'subject:Check': 0.95 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=CvRCCSMD c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=EBOSESyhAAAA:8 a=IkcTkHD0fZMA:10 a=pGLkceISAAAA:8 a=aDt1bbRhOUiSFdZW1EcA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.5.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:101071 On 2015-12-31 18: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? > I would use .split and then ''.join: >>> ''.join(' [ { ....'.split()) '[{....' It might be faster if you provide a maximum for the number of splits: >>> ''.join(' [ { ....'.split(None, 1)) '[{ ....'