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

  • Home
  • SEARCH
  • 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 8797471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:51:52+00:00 2026-06-13T23:51:52+00:00

Having a problem getting scanString to work in a case where parseString gives a

  • 0

Having a problem getting scanString to work in a case where parseString gives a correct result.

This sequence works:

alpha_rev = pyp.Word(pyp.alphas, max=2)
num_rev = pyp.Word('123456789', max=2)
space = pyp.White(ws=" ").suppress()

revisionExpr = (
    pyp.StringStart().leaveWhitespace() +
    space +
    pyp.Combine(alpha_rev + 
    pyp.Optional(num_rev)("rev"))
    )

rev_string = ' K        WI, This is the title'

for match_str, start, end in (
    revisionExpr.scanString(rev_string, maxMatches=1)):
    print match_str

['K']

Sometimes there is a “Rev” or “Rev.” before the revision; this fails:

revisionExpr = (
    pyp.StringStart().leaveWhitespace() +
    space +
    pyp.Combine(alpha_rev + 
    pyp.Optional(num_rev)("rev"))
    |
    pyp.CaselessLiteral("Rev") + pyp.Optional('.') + 
    pyp.Combine(alpha_rev + 
    pyp.Optional(num_rev)("rev"))
    )

for match_str, start, end in (
    revisionExpr.scanString(rev_string, maxMatches=1)):
    print match_str

print match_str
NameError: name 'match_str' is not defined

Why is “|” causing the the match to fail? Note that this works with both the first and second example:

revisionTokens = revisionExpr.parseString(rev_string)

If I extract the second part of the last example (after the “|”) into a form like the first example, it works if I add “Rev.” in front of the “K” in rev_string. Unfortunately, the leading whitespace in the first expression is necessary to uniquely identifiy the revision string, otherwise, in this example, “WI” would match.

I’m trying to use scanString instead of parseString because it returns the starting and ending positions of the match which helps with some later processing.

  • 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-06-13T23:51:53+00:00Added an answer on June 13, 2026 at 11:51 pm

    The problem is that your “or” operator (“|”) is only looking at the elements directly to the left and right of it. You have not grouped your grammar elements correctly. Here is your grammar broken down a bit more:

    left_expr = pyp.Combine(alpha_rev + pyp.Optional(num_rev)("rev")
    right_expr = pyp.CaselessLiteral("Rev")
    
    joined_expr = left_expr | right_expr
    
    final_expr = (pyp.StringStart().leaveWhitespace() +
        space +
        joined_expr +
        pyp.Optional('.') +
        pyp.Combine(alpha_rev +
          pyp.Optional(num_rev)("rev"))
        )
    

    As you can see, this isn’t quite what you wanted – it’s going to look for either the text “Rev” or the actual revision, followed by another revision. A fixed version of the expression is below:

    revisionExpr = (
        pyp.StringStart().leaveWhitespace() +
        space +
        (
            pyp.Combine(alpha_rev +
                pyp.Optional(num_rev)("rev")
            )
            |
            (
                pyp.CaselessLiteral("Rev") + 
                pyp.Optional('.') +
                pyp.Combine(alpha_rev +
                    pyp.Optional(num_rev)("rev"))
            )
        )
    )
    

    However, you can make your grammar a bit more concise:

    revisionExpr = (
        pyp.StringStart().leaveWhitespace() +
        space +
        pyp.Suppress(
            pyp.Optional(
                pyp.CaselessLiteral("Rev") + 
                pyp.Optional('.')
            )
        ) +
        pyp.Combine(
            alpha_rev +
            pyp.Optional(num_rev)("rev")
        )
    )
    

    In this version, you only mark the “Rev.” text as optional, in the position where it is expected, rather than giving the parse the option to parse just a revision OR “Rev.” + a revision. This avoids any issues arising from using the “|” operator at all.

    Don’t forget that PyParsing uses operator overloading to provide nicer syntax, if the syntax causes confusion (like in this scenario) you may be better off just using the long-form method calls, like “pyp.Or(a, b)”.

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

Sidebar

Related Questions

I'm having a problem getting this test case to work. Can anyone point me
I am having a problem getting this COMPARE macro to work. Any ideas on
I'm having a problem getting this functionality to work. Situation is: User Drags an
I am having problem getting my tooltip to work when using the jQuery load()
I am having problem getting this piece of code to run. The class is
I'm having a problem getting this type conversion working correctly. My guess is the
I'm having a problem getting the BlendState on the graphics device to work properly
I'm having a problem getting a custom membership provider to work in SharePoint. The
I'm having a problem getting my CSS to work properly in Safari. It was
I'm having a problem getting more memory out of PHP. This is the error

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.