Skip to content
\n

I want to restrict the characters in it, so I replaced it with this:

\n
string: pp.Word = pp.Word(\n    pp.printables, exclude_chars=meta_chars, min=2, as_keyword=True\n)\ndouble_quoted_string: pp.ParserElement = pp.Combine(\n    double_quote + string + double_quote\n)\nsingle_quoted_string: pp.ParserElement = pp.Combine(\n    single_quote + string + single_quote\n)\nstring_def: pp.ParserElement = (\n    double_quoted_string | single_quoted_string\n)\n
\n

This throws an exception on the same input though.

\n

I'm guessing I'm missing something subtle. Appreciate any tips on how to accomplish the goal.

\n

(Thanks for the wonderful package, Paul!)

","upvoteCount":1,"answerCount":3,"acceptedAnswer":{"@type":"Answer","text":"

This gave me just enough to go on that I figured out the problem: I wasn't excluding single or double quotes from the word, so my string var was eating them.

\n

This:

\n
quotes: str = \"\\\"'\"\nstring: Word = Word(printables, exclude_chars=quotes + meta_chars, min=2)\ndouble_quoted_string: ParserElement = Combine(double_quote + string + double_quote)\nsingle_quoted_string: ParserElement = Combine(single_quote + string + single_quote)\nstring_def: ParserElement = double_quoted_string | single_quoted_string\n
\n

is how to replace this and only include printable characters (except quotes and meta characters):

\n
string_def: QuotedString = QuotedString('\"', unquote_results=False) | QuotedString(\n    \"'\", unquote_results=False\n)\n\nThanks again ...\n
","upvoteCount":1,"url":"https://github.com/pyparsing/pyparsing/discussions/460#discussioncomment-4988984"}}}
Discussion options

You must be logged in to vote

This gave me just enough to go on that I figured out the problem: I wasn't excluding single or double quotes from the word, so my string var was eating them.

This:

quotes: str = "\"'"
string: Word = Word(printables, exclude_chars=quotes + meta_chars, min=2)
double_quoted_string: ParserElement = Combine(double_quote + string + double_quote)
single_quoted_string: ParserElement = Combine(single_quote + string + single_quote)
string_def: ParserElement = double_quoted_string | single_quoted_string

is how to replace this and only include printable characters (except quotes and meta characters):

string_def: QuotedString = QuotedString('"', unquote_results=False) | QuotedString(
    "'", unquote…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by alecramsay
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants