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 503953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:26:48+00:00 2026-05-13T06:26:48+00:00

Pythonistas: Suppose you want to parse the following string using Pyparsing: ‘ABC_123_SPEED_X 123’ were

  • 0

Pythonistas:

Suppose you want to parse the following string using Pyparsing:

'ABC_123_SPEED_X 123'

were ABC_123 is an identifier; SPEED_X is a parameter, and 123 is a value. I thought of the following BNF using Pyparsing:

Identifier = Word( alphanums + '_' )
Parameter = Keyword('SPEED_X') or Keyword('SPEED_Y') or Keyword('SPEED_Z')
Value = # assume I already have an expression valid for any value
Entry = Identifier + Literal('_') + Parameter + Value
tokens = Entry.parseString('ABC_123_SPEED_X 123')
#Error: pyparsing.ParseException: Expected "_" (at char 16), (line:1, col:17)

If I remove the underscore from the middle (and adjust the Entry definition accordingly) it parses correctly.

How can I make this parser be a bit lazier and wait until it matches the Keyword (as opposed to slurping the entire string as an Identifier and waiting for the _, which does not exist.

Thank you.

[Note: This is a complete rewrite of my question; I had not realized what the real problem was]

  • 1 1 Answer
  • 3 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-13T06:26:48+00:00Added an answer on May 13, 2026 at 6:26 am

    I based my answer off of this one, since what you’re trying to do is get a non-greedy match. It seems like this is difficult to make happen in pyparsing, but not impossible with some cleverness and compromise. The following seems to work:

    from pyparsing import *
    Parameter = Literal('SPEED_X') | Literal('SPEED_Y') | Literal('SPEED_Z')
    UndParam = Suppress('_') + Parameter
    Identifier = SkipTo(UndParam)
    Value = Word(nums)
    Entry = Identifier + UndParam + Value
    

    When we run this from the interactive interpreter, we can see the following:

    >>> Entry.parseString('ABC_123_SPEED_X 123')
    (['ABC_123', 'SPEED_X', '123'], {})
    

    Note that this is a compromise; because I use SkipTo, the Identifier can be full of evil, disgusting characters, not just beautiful alphanums with the occasional underscore.

    EDIT: Thanks to Paul McGuire, we can concoct a truly elegant solution by setting Identifier to the following:

    Identifier = Combine(Word(alphanums) +
            ZeroOrMore('_' + ~Parameter + Word(alphanums)))
    

    Let’s inspect how this works. First, ignore the outer Combine; we’ll get to this later. Starting with Word(alphanums) we know we’ll get the 'ABC' part of the reference string, 'ABC_123_SPEED_X 123'. It’s important to note that we didn’t allow the “word” to contain underscores in this case. We build that separately in to the logic.

    Next, we need to capture the '_123' part without also sucking in '_SPEED_X'. Let’s also skip over ZeroOrMore at this point and return to it later. We start with the underscore as a Literal, but we can shortcut with just '_', which will get us the leading underscore, but not all of '_123'. Instictively, we would place another Word(alphanums) to capture the rest, but that’s exactly what will get us in trouble by consuming all of the remaining '_123_SPEED_X'. Instead, we say, “So long as what follows the underscore is not the Parameter, parse that as part of my Identifier. We state that in pyparsing terms as '_' + ~Parameter + Word(alphanums). Since we assume we can have an arbitrary number of underscore + WordButNotParameter repeats, we wrap that expression a ZeroOrMore construct. (If you always expect at least underscore + WordButNotParameter following the initial, you can use OneOrMore.)

    Finally, we need to wrap the initial Word and the special underscore + Word repeats together so that it’s understood they are contiguous, not separated by whitespace, so we wrap the whole expression up in a Combine construct. This way 'ABC _123_SPEED_X' will raise a parse error, but 'ABC_123_SPEED_X' will parse correctly.

    Note also that I had to change Keyword to Literal because the ways of the former are far too subtle and quick to anger. I do not trust Keywords, nor could I get matching with them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to compare reading lines of string input from stdin using Python and
I'm looking to store time-based counters using Redis, in a Python project. I want
How do pythonistas print a number as words, like the equivalent of the Common
Specifically, I'm trying to use a string to arbitrairly filter the ORM. I've tried
Yes, I know it's considered lazy by the non-Pythonistas. The reason I ask is
When I install bcfg2 - http://bcfg2.org using pip install git+git://git.mcs.anl.gov/bcfg2.git#egg=Bcfg2 or pip install bcfg2
I stumbled across the following warning when I was reading Code Like a Pythonista:
Good day pythonistas and the rest of the coding crowd, I have two QMainWindows
I often struggle with the following if/else syntax and I was hoping some people
I am looking for a good Tree data structure class. I have come across

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.