Newbie: Check first two non-whitespace characters

cassius.fechter at gmail.com cassius.fechter at gmail.com
Thu Dec 31 14:21:18 EST 2015


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 at 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




More information about the Python-list mailing list