Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7608673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:58:41+00:00 2026-05-31T00:58:41+00:00

So far I have tried using ‘setParseAction’ to get the location of matched tokens,

  • 0

So far I have tried using ‘setParseAction’ to get the location of matched tokens, but it doesn’t work as I expect sometimes. Take this code as an example:

>>> from pyparsing import *
>>> 
>>> Z = Literal('0')
>>> POINT = Literal('.')
>>> BIN_DIGITS = Word('01')
>>> OCT_DIGITS = Word('01234567')
>>> DEC_DIGITS = Word('0123456789')
>>> HEX_DIGITS = Word('0123456789abcdefABCDEF')
>>> DEC_INT = DEC_DIGITS.setParseAction(lambda t: int(t[0]))
>>> BIN_INT = Combine(Z + ((Literal('b') | 'B')) + BIN_DIGITS).\
...         setParseAction(lambda t: int(t[0], 2))
>>> OCT_INT = Combine(Z + ((Literal('o') | 'O')) + OCT_DIGITS).\
...         setParseAction(lambda t: int(t[0], 8))
>>> HEX_INT = Combine(Z + ((Literal('x') | 'X')) + HEX_DIGITS).\
...         setParseAction(lambda t: int(t[0], 16))
>>> INTEGER = HEX_INT | OCT_INT | BIN_INT | DEC_INT
>>> EXP = Combine(CaselessLiteral('E') + Optional(Literal('+') | '-') + DEC_INT)
>>> POINT_FLOAT = Combine(Optional(DEC_INT) + POINT + DEC_INT) | \
...         Combine(DEC_INT + POINT)
>>> EXP_FLOAT = Combine(DEC_INT + EXP) | Combine(POINT_FLOAT + EXP)
>>> FLOAT = (EXP_FLOAT | POINT_FLOAT).setParseAction(lambda t: float(t[0]))
>>> 
>>> 
>>> def p(s, l, t):
...     print 'Location of %s:  %s' % (t[0], l,)
... 
>>> 
>>> NUMBER = (FLOAT | INTEGER).setParseAction(p)
>>> NUMBER.parseString('    12345')
Location of 12345:  0
([12345], {})
>>> NUMBER.parseString(' 12345')
Location of 12345:  0
([12345], {})
>>> NUMBER.parseString('12345')
Location of 12345:  0
([12345], {})

Location is always 0 no matter where I position the number ‘12345’ in the string. However if I try:

>>> LITERAL = Literal('someword').setParseAction(p)
>>> LITERAL.parseString('    someword')
Location of someword:  4
(['someword'], {})
>>> LITERAL.parseString(' someword')
Location of someword:  1
(['someword'], {})
>>> LITERAL.parseString('someword')
Location of someword:  0
(['someword'], {})

It works as expected. What am I doing wrong in the first example?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:58:43+00:00Added an answer on May 31, 2026 at 12:58 am

    Ok, thanks to Paul’s tip, I managed work around this problem by combining the ‘Or’ expression with a supressed whitespace expression, and using it to parse the string. Heres the end result:

    >>> from pyparsing import *
    >>> 
    >>> def p(s,l,t):
    ...     print 'Location of %s: %s' % (s, l,)
    ... 
    >>> Z = Literal('0')
    >>> POINT = Literal('.')
    >>> BIN_DIGITS = Word('01')
    >>> OCT_DIGITS = Word('01234567')
    >>> DEC_DIGITS = Word('0123456789')
    >>> HEX_DIGITS = Word('0123456789abcdefABCDEF')
    >>> DEC_INT = DEC_DIGITS.copy().setParseAction(lambda t: int(t[0]))
    >>> BIN_INT = Combine(Z + ((Literal('b') | 'B')) + BIN_DIGITS).\
    ...         setParseAction(lambda t: int(t[0], 2))
    >>> OCT_INT = Combine(Z + ((Literal('o') | 'O')) + OCT_DIGITS).\
    ...         setParseAction(lambda t: int(t[0], 8))
    >>> HEX_INT = Combine(Z + ((Literal('x') | 'X')) + HEX_DIGITS).\
    ...         setParseAction(lambda t: int(t[0], 16))
    >>> INTEGER = HEX_INT | OCT_INT | BIN_INT | DEC_INT
    >>> EXP = Combine(CaselessLiteral('E') + Optional(Literal('+') | Literal('-')) + DEC_DIGITS)
    >>> POINT_FLOAT = Combine(Optional(DEC_DIGITS) + POINT + DEC_DIGITS) | \
    ...         Combine(DEC_DIGITS + POINT)
    >>> EXP_FLOAT = Combine(DEC_DIGITS + EXP) | Combine(POINT_FLOAT + EXP)
    >>> FLOAT = (EXP_FLOAT | POINT_FLOAT).setParseAction(lambda t: float(t[0]))
    >>> NUMBER = (FLOAT | INTEGER).setParseAction(p)
    >>> NUMBER2 = Combine(ZeroOrMore(White(exact=1)).suppress() + NUMBER)
    >>> 
    >>> NUMBER2.parseString('    12345')
    Location of     12345: 4
    (['12345'], {})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried several things so far, but I haven't had much luck yet.
Here is what I have tried thus far: > cd /Applications/MAMP/bin/php5/bin/ > ./pear channel-discover
So far I have the expression: http://[^\.]*\.mydomain\.com/((.*?)) Which matches... http://www.mydomain.com/Images/favicon.ico But I really dont
So far I have been creating Web Portal but recently I had a request
So far I have learned about generating thread dump and heap dump using jstack
I have so far tried Editing php.ini to allow_url_open = on; I have tried
I'm a new user to knockout.js and so far have been very impressed with
So far I have only been updated a view from within its controller. I
So far I have managed to write some code that should print the source
So far i have this: mov ah,02h mov cl,11001100001111011101000b ;6,692,584 in dec mov dl,0

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.