By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,814 Members | 1,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,814 IT Pros & Developers. It's quick & easy.

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

P: n/a
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!

Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ph*****************@yahoo.com 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
Jul 19 '05 #2

P: n/a
Be careful, Kent. You may get tagged as "the new pyparsing guy." :)

-- Paul

Jul 19 '05 #3

P: n/a
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
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.