Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

FAQ Frequently Asked Questions

Paul McGuire edited this pageJun 20, 2019 ·5 revisions

(in progress)

Is your question not here? Try searching for thepyparsing tag on StackOverflow, or requesting an addition to this page on thepyparsing subreddit

  1. Why do I only get the last value for a results name, when there are multiple matches?
  2. What is the difference betweenOr andMatchFirst?
  3. How do I evaluate the results from parsinginfixNotation?

Why do I only get the last value for a results name, when there are multiple matches?

Results names by default use similar logic as Python dicts: if there are multiple values assigned for the same key, only the last assigned value is kept.

However, this behavior can be overridden, depending how the parser defines the results names.

If using the fullexpr.setResultsName("XYZ") form, addlistAllMatches=True argument. This tells pyparsing to keep a list of all parsed values and return them as a list.

If using the short-cutexpr("XYZ") form, add a'*' to the end of the name:expr("XYZ*"). This is equivalent to settinglistAllMatches to True.

The trailing'*' is there insetResultsName for those cases where you use the short form ofsetResultsName:expr("name*") vsexpr.setResultsName("name", listAllMatches=True). If you prefer callingsetResultsName, then do not use the'*' notation, but instead pass thelistAllMatches argument.

Short answer:Or evaluates all the alternatives and selects the longest match.MatchFirst evaluates the alternatives left-to-right and immediately returns when it finds the first match, even if a later alternative might be a better match.MatchFirst can result in faster parsers, and the issues with incorrectly selecting a shorter match can usually be addressed with careful order of the alternative selections, or in using expressions that do not overlap one another. More info on thePerformance Tips page.

This will take a very long answer. For now, refer to SimpleBool.py in the examples for how to parse and evaluate infix of Boolean NOT, AND, and OR expressions; and eval_arith.py for 5-function arithmetic.

Clone this wiki locally


[8]ページ先頭

©2009-2025 Movatter.jp