Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Random832 Newsgroups: comp.lang.python Subject: Re: Newbie: Check first two non-whitespace characters Date: Thu, 31 Dec 2015 20:31:16 -0500 Lines: 14 Message-ID: References: <240ab049-68a0-4a00-b911-d58cae9bfbcf@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain X-Trace: news.uni-berlin.de dSkhitdPSFTTwKU8UqtMnQkGCEZUWvD9KYdZD8RKJoGg== Cancel-Lock: sha1:q4BqL5qHJFT5qOjb7MnitcQ1tW8= 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; 'ideally': 0.04; 'json': 0.05; 'subject:two': 0.07; 'parsed': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:characters': 0.09; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'subject:non': 0.16; 'later': 0.16; 'string': 0.17; 'element': 0.18; 'received:166': 0.18; 'string,': 0.18; 'header:User- Agent:1': 0.26; '(which': 0.26; 'header:X-Complaints-To:1': 0.26; 'be:': 0.29; 'consisting': 0.29; 'list': 0.34; 'could': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'space': 0.40; 'email addr:gmail.com': 0.62; 'validate': 0.76; 'subject:Check': 0.95 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 166.175.190.4 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (darwin) 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:101095 otaksoftspamtrap@gmail.com writes: > 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? Is it an arbitrary string, or is it a JSON object consisting of a list whose first element is a dictionary? Because if you're planning on reading it as a JSON object later you could just validate the types after you've parsed it.