Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to use pyparsing for identifiers that start with a constant string

Reply
Thread Tools

how to use pyparsing for identifiers that start with a constant string

 
 
phil_nospam_schmidt@yahoo.com
Guest
Posts: n/a
 
      06-14-2005
I am scanning text that has identifiers with a constant prefix string
followed by alphanumerics and underscores. I can't figure out, using
pyparsing, how to match for this. The example expression below seems to
be looking for whitespace between the 'atod' and the rest of the
identifier.

identifier_atod = 'atod' + pp.Word('_' + pp.alphanums)

How can I get pyparsing to match 'atodkj45k' and 'atod_asdfaw', but not
'atgdkasdjfhlksj' and 'atod asdf4er', where the first four characters
must be 'atod', and not followed by whitespace?

Thanks!

 
Reply With Quote
 
 
 
 
Kent Johnson
Guest
Posts: n/a
 
      06-14-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I am scanning text that has identifiers with a constant prefix string
> followed by alphanumerics and underscores. I can't figure out, using
> pyparsing, how to match for this. The example expression below seems to
> be looking for whitespace between the 'atod' and the rest of the
> identifier.
>
> identifier_atod = 'atod' + pp.Word('_' + pp.alphanums)
>
> How can I get pyparsing to match 'atodkj45k' and 'atod_asdfaw', but not
> 'atgdkasdjfhlksj' and 'atod asdf4er', where the first four characters
> must be 'atod', and not followed by whitespace?


Here is one way using pyparsing.Combine:

>>> from pyparsing import *
>>> tests = [ 'atodkj45k', 'atod_asdfaw', 'atgdkasdjfhlksj', 'atod asdf4er']
>>> ident = Combine(Literal('atod') + Word('_' + alphanums))
>>> for t in tests:

... try:
... print ident.parseString(t)
... except:
... print 'No match', t
...
['atodkj45k']
['atod_asdfaw']
No match atgdkasdjfhlksj
No match atod asdf4er
>>>


Kent
 
Reply With Quote
 
 
 
 
Paul McGuire
Guest
Posts: n/a
 
      06-15-2005
Be careful, Kent. You may get tagged as "the new pyparsing guy."

-- Paul

 
Reply With Quote
 
Kent Johnson
Guest
Posts: n/a
 
      06-15-2005
Paul McGuire wrote:
> Be careful, Kent. You may get tagged as "the new pyparsing guy."


Yeah, I was a little surprised I beat you to that one

Kent
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
concatenate a constant to constant string using macros sinbad C Programming 7 06-19-2008 05:28 PM
Binding identifiers to known or unknown identifiers latashag@live.com Java 1 04-22-2008 04:54 AM
"use constant X=>(1,2);" or "use constant X=>[1,2];"? Victor Porton Perl Misc 7 12-11-2007 09:10 PM



Advertisments