English 中文(简体)
将变形字符串以负外观分隔
原标题:pyparsing transform_string with negative lookahead

我试图用 $varname 语法来实施简单的 shell 风格字符串变量内插, 使用 pyparsing 。 例如, 如果我有一个变量 < code> foo , 值为 < code> bar , 然后将 < code > > x $foo x " 转换为 < code> x bar x "

但是,当 $ 前面有一个反斜线时,我想防止变量内插。 因此, "x $foo x" 应该留在 x $foo x" 。 我使用 pyparsing s transform_string 。 我试图添加负面头,以避免对以 开头的字符串进行解析。 但是它不起作用 。

import pyparsing as pp

vars = {"foo": "bar"}
interp = pp.Combine(
    ~pp.Literal("\")
    + pp.Literal("$").suppress()
    + pp.common.identifier.set_parse_action(lambda t: vars[t[0]])
)
print(interp.transform_string("x $foo x"))
print(interp.transform_string("x \$foo x"))

这一产出:

x bar x
x ar x

但我喜欢它输出:

x bar x
x $foo x

我怀疑在剖析器开始时的负外观与 transform_string 无效, 因为它仍然可以找到一个子字符串, 而没有该子字符串 。 但我不确定如何解决这个问题 。

问题回答

再看一遍,我意识到了几件事:

  1. $ actually needs to be replaced with $ in the output string to get standard string escaping behavior.
  2. Once I modify the parser to consume the $ and replace it with a $, everything works properly.

我对代码进行了更新,以便:

import pyparsing as pp

vars = {"foo": "bar"}
interp = pp.Combine(pp.Literal("\").suppress() + pp.Char(pp.printables)) | pp.Combine(
    pp.Literal("$").suppress()
    + pp.common.identifier.set_parse_action(lambda t: vars[t[0]])
)
print(interp.transform_string("x $foo x"))
print(interp.transform_string("x $foo x"))

这给了我正确的输出:

x bar x
x $foo x




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签