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

Restricting characters in a quoted string#460

Answeredbyalecramsay
alecramsay asked this question inQ&A
Discussion options

I have a double- or single-quoted string like this which works great:

string_def: pp.QuotedString = pp.QuotedString(    '"', unquote_results=False) | pp.QuotedString("'", unquote_results=False)

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

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

This throws an exception on the same input though.

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

(Thanks for the wonderful package, Paul!)

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

What is in meta_chars? And what is the input that is failing that you expect to succeed?

You must be logged in to vote
0 replies
Comment options

If there are spaces in your quoted strings, then the expression you've written won't match them because printables does not include any whitespace characters.

You must be logged in to vote
0 replies
Comment options

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_results=False)Thanks again ...
You must be logged in to vote
0 replies
Answer selected byalecramsay
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@alecramsay@ptmcg

[8]ページ先頭

©2009-2025 Movatter.jp