Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #101076

Re: Newbie: Check first two non-whitespace characters

Newsgroups comp.lang.python
Date 2015-12-31 11:21 -0800
References <240ab049-68a0-4a00-b911-d58cae9bfbcf@googlegroups.com> <568579DF.50805@gmail.com> <mailman.119.1451588711.11925.python-list@python.org>
Message-ID <3f355b97-3a94-41af-8624-9d244eb555a3@googlegroups.com> (permalink)
Subject Re: Newbie: Check first two non-whitespace characters
From cassius.fechter@gmail.com

Show all headers | View raw


Thanks much to both of you!


On Thursday, December 31, 2015 at 11:05:26 AM UTC-8, Karim wrote:
> On 31/12/2015 19:54, Karim wrote:
> >
> >
> > On 31/12/2015 19:18, snailpail@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
> 
> Sorry the method to parse a string is parseString not parse, please 
> replace by this line:
> 
>  >>> result = mystring.parseString(' [ { .... I am learning pyparsing' )
> 
> Regards

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Newbie: Check first two non-whitespace characters otaksoftspamtrap@gmail.com - 2015-12-31 10:18 -0800
  Re: Newbie: Check first two non-whitespace characters MRAB <python@mrabarnett.plus.com> - 2015-12-31 18:38 +0000
  Re: Newbie: Check first two non-whitespace characters Karim <kliateni@gmail.com> - 2015-12-31 19:54 +0100
  Re: Newbie: Check first two non-whitespace characters Karim <kliateni@gmail.com> - 2015-12-31 20:05 +0100
    Re: Newbie: Check first two non-whitespace characters cassius.fechter@gmail.com - 2015-12-31 11:21 -0800
  Re: Newbie: Check first two non-whitespace characters Cory Madden <csmadden@gmail.com> - 2015-12-31 10:56 -0800
  Re: Newbie: Check first two non-whitespace characters Denis McMahon <denismfmcmahon@gmail.com> - 2015-12-31 20:35 +0000
  Re: Newbie: Check first two non-whitespace characters Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-12-31 23:25 +0000
    Re: Newbie: Check first two non-whitespace characters Steven D'Aprano <steve@pearwood.info> - 2016-01-01 12:12 +1100
  Re: Newbie: Check first two non-whitespace characters Steven D'Aprano <steve@pearwood.info> - 2016-01-01 12:23 +1100
  Re: Newbie: Check first two non-whitespace characters Random832 <random832@fastmail.com> - 2015-12-31 20:31 -0500
  Re: Newbie: Check first two non-whitespace characters Jussi Piitulainen <harvesting@is.invalid> - 2016-01-01 10:16 +0200
  Re: Newbie: Check first two non-whitespace characters Karim <kliateni@gmail.com> - 2016-01-01 10:43 +0100

csiph-web