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

Discussion in 'Python' started by phil_nospam_schmidt@yahoo.com, Jun 14, 2005.

  1. Guest

    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!
     
    , Jun 14, 2005
    #1
    1. Advertising

  2. Kent Johnson Guest

    Re: how to use pyparsing for identifiers that start with a constantstring

    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
     
    Kent Johnson, Jun 14, 2005
    #2
    1. Advertising

  3. Paul McGuire Guest

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

    -- Paul
     
    Paul McGuire, Jun 15, 2005
    #3
  4. Kent Johnson Guest

    Re: how to use pyparsing for identifiers that start with a constantstring

    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
     
    Kent Johnson, Jun 15, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,127
  2. Replies:
    1
    Views:
    367
    Roedy Green
    Apr 22, 2008
  3. sinbad
    Replies:
    7
    Views:
    679
    sinbad
    Jun 19, 2008
  4. Jeremy Banks
    Replies:
    12
    Views:
    450
    Steven D'Aprano
    Apr 24, 2009
  5. Victor Porton
    Replies:
    7
    Views:
    116
    TonyV
    Dec 11, 2007
Loading...

Share This Page