- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
到目前为止,我已尝试使用“setParseAction”来获取匹配标记的位置,但它有时无法像我预期的那样工作。以此代码为例:
>>> from pyparsing import *
>>>
>>> Z = Literal('0')
>>> POINT = Literal('.')
>>> BIN_DIGITS = Word('01')
>>> OCT_DIGITS = Word('01234567')
>>> DEC_DIGITS = Word('0123456789')
>>> HEX_DIGITS = Word('0123456789abcdefABCDEF')
>>> DEC_INT = DEC_DIGITS.setParseAction(lambda t: int(t[0]))
>>> BIN_INT = Combine(Z + ((Literal('b') | 'B')) + BIN_DIGITS).\
... setParseAction(lambda t: int(t[0], 2))
>>> OCT_INT = Combine(Z + ((Literal('o') | 'O')) + OCT_DIGITS).\
... setParseAction(lambda t: int(t[0], 8))
>>> HEX_INT = Combine(Z + ((Literal('x') | 'X')) + HEX_DIGITS).\
... setParseAction(lambda t: int(t[0], 16))
>>> INTEGER = HEX_INT | OCT_INT | BIN_INT | DEC_INT
>>> EXP = Combine(CaselessLiteral('E') + Optional(Literal('+') | '-') + DEC_INT)
>>> POINT_FLOAT = Combine(Optional(DEC_INT) + POINT + DEC_INT) | \
... Combine(DEC_INT + POINT)
>>> EXP_FLOAT = Combine(DEC_INT + EXP) | Combine(POINT_FLOAT + EXP)
>>> FLOAT = (EXP_FLOAT | POINT_FLOAT).setParseAction(lambda t: float(t[0]))
>>>
>>>
>>> def p(s, l, t):
... print 'Location of %s: %s' % (t[0], l,)
...
>>>
>>> NUMBER = (FLOAT | INTEGER).setParseAction(p)
>>> NUMBER.parseString(' 12345')
Location of 12345: 0
([12345], {})
>>> NUMBER.parseString(' 12345')
Location of 12345: 0
([12345], {})
>>> NUMBER.parseString('12345')
Location of 12345: 0
([12345], {})
无论我将数字“12345”放在字符串中的什么位置,位置始终为 0。但是,如果我尝试:
>>> LITERAL = Literal('someword').setParseAction(p)
>>> LITERAL.parseString(' someword')
Location of someword: 4
(['someword'], {})
>>> LITERAL.parseString(' someword')
Location of someword: 1
(['someword'], {})
>>> LITERAL.parseString('someword')
Location of someword: 0
(['someword'], {})
它按预期工作。我在第一个示例中做错了什么?
最佳答案
好的,多亏了 Paul 的提示,我通过将“Or”表达式与压缩的空白表达式相结合,并使用它来解析字符串,设法解决了这个问题。这是最终结果:
>>> from pyparsing import *
>>>
>>> def p(s,l,t):
... print 'Location of %s: %s' % (s, l,)
...
>>> Z = Literal('0')
>>> POINT = Literal('.')
>>> BIN_DIGITS = Word('01')
>>> OCT_DIGITS = Word('01234567')
>>> DEC_DIGITS = Word('0123456789')
>>> HEX_DIGITS = Word('0123456789abcdefABCDEF')
>>> DEC_INT = DEC_DIGITS.copy().setParseAction(lambda t: int(t[0]))
>>> BIN_INT = Combine(Z + ((Literal('b') | 'B')) + BIN_DIGITS).\
... setParseAction(lambda t: int(t[0], 2))
>>> OCT_INT = Combine(Z + ((Literal('o') | 'O')) + OCT_DIGITS).\
... setParseAction(lambda t: int(t[0], 8))
>>> HEX_INT = Combine(Z + ((Literal('x') | 'X')) + HEX_DIGITS).\
... setParseAction(lambda t: int(t[0], 16))
>>> INTEGER = HEX_INT | OCT_INT | BIN_INT | DEC_INT
>>> EXP = Combine(CaselessLiteral('E') + Optional(Literal('+') | Literal('-')) + DEC_DIGITS)
>>> POINT_FLOAT = Combine(Optional(DEC_DIGITS) + POINT + DEC_DIGITS) | \
... Combine(DEC_DIGITS + POINT)
>>> EXP_FLOAT = Combine(DEC_DIGITS + EXP) | Combine(POINT_FLOAT + EXP)
>>> FLOAT = (EXP_FLOAT | POINT_FLOAT).setParseAction(lambda t: float(t[0]))
>>> NUMBER = (FLOAT | INTEGER).setParseAction(p)
>>> NUMBER2 = Combine(ZeroOrMore(White(exact=1)).suppress() + NUMBER)
>>>
>>> NUMBER2.parseString(' 12345')
Location of 12345: 4
(['12345'], {})
关于python - 如何使用 pyparsing 获取匹配标记的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9618950/
PEG论文描述了两种语义谓词解析表达式: 和谓词&e 不是谓词!e pyparsing 是否支持 And 谓词?或者这只是排序解析表达式的同义词?在这种情况下,它应该等同于 And 类。对吗? Not
我有以下语法和测试用例: from pyparsing import Word, nums, Forward, Suppress, OneOrMore, Group #A grammar for a
我想用这些条件解析带有嵌套括号的字符串: 元素由逗号分隔 ,或吧 | . 嵌套括号元素可能是单个字母数字或另一个嵌套括号。 每个嵌套括号元素由条 | 连接字面量导致创建一个新序列,该序列将先前的序列元
有以下我要解析的字符串: ((K00134,K00150) K00927,K11389) (K00234,K00235) 每个步骤由空格分隔,交替由逗号表示。我被困在字符串的第一部分,括号内有一个空格
所以这是取自 fourFn.py 的解析器: from pyparsing import ( Literal, Word, Group, Forward, al
所以我在制作解析器时,发现了一个问题。事实上,为了解析数字,我有: from pyparsing import Word, nums n = Word(nums) 这适用于没有千位分隔符的数字。例如,
我有一个简单的数据集,可以使用如下行进行解析: R1 (a/30) to R2 (b/30), metric 30 我需要从上面得到的唯一数据如下: R1, a, 30, R2, 192.168.0.
我正在尝试使用 pyparsing 来构建一个解析器,该解析器将匹配任意嵌套的一组括号内的所有文本。如果我们考虑这样的字符串: "[A,[B,C],[D,E,F],G] Random Middle t
from pyparsing import * class AParseActionHolder(object): def __call__(self, string, index, t):
我正在寻找一种改进使用pyparsing构建的解析器性能的方法。我阅读了关于packrat的解析,看来这确实可以帮助解析器提高性能。但是,当我启用packrat解析时,性能会变差!如果没有packra
我正在尝试使用一些特殊规则(例如相邻值和邻近值)创建 bool 查询解析器。到目前为止我创建的规则是 ## DEFINITIONS OF SYMBOLS ### NEAR = CaselessLite
我编写此脚本是为了使用类似于序言的语法来解析语句,将连接词视为具有优先级的运算符: import pyparsing as pyp alphabet = "abcdefghijklmnopqrstuv
我有一个来自游戏的文件,我正在尝试解析它。以下是摘录: id: 50 #Survival Stage bound: 1500 # phase 0 bandi
我很难理解允许在参数名称中使用“\”的语法(例如 net\)。然而,“\”也可用作续行(参见例 2)。 Ex1 工作正常,但 linebreak 和 identifier 变量之间存在冲突。 Ex1:
我正在尝试学习 pyparsing。这听起来很有希望,并且用于文本处理会很有趣。无论如何,这是我的问题: 我有一个类(class)名称列表。例如, courselist = ["Project Bas
我想利用 cStyleComment 变量,但我想专门处理它们,而不是仅仅忽略这些注释。有什么方法可以让 pyparsing 在将其识别为注释的输入片段上调用我的处理程序,然后再将其丢弃吗? 我正在处
我今天早些时候发布了一个关于同一问题的问题,但由于该问题的解决方案是切换库,而我现在对另一个库有问题,我想我会提出另一个问题......希望没关系。 所以我不想匹配以下标记: ... 我不
我有一个如下所示的数据: data = 'person(firstame="bob", lastname="stewart", dob="2010-0206", hobbies=["reading,
我正在通过解析文件 output=wilcard.parseFile(myfile) print output 我确实只得到了字符串的第一个匹配。 我有一个大的配置文件需要解析,其中的“条目”用大括号
我需要解析以下三行: Uptime is 1w2d Last reset at 23:05:56 Reason: reload 但最后两行并不总是存在,在第一次重新启动之前输出可能如下所示: Up
我是一名优秀的程序员,十分优秀!