仅匹配出现在解析字符串右半部分的元素
Match an element only when it appears in the right half of the parsing string
问题
我想创建一个解析器来匹配像
这样的字符串
"alpha beta 123 cpart"
----^----- -^- --^--
A: B: C:
alphanums num alpha
然而,B 部分仅当出现在字符串的后半部分(即字符串中点的 'right' 时)才会匹配。
所以上面的示例字符串应该被解析成以下部分:
A: ['alpha', 'beta']
B: '123'
C: ['cpart']
但是字符串"123 alpha beta cpart"
应该解析成:
A: '123 alpha beta cpart'
B: ''
C: ''
使用 pyparsing 的第一次近似
作为 pyparsing
的起点,我尝试使用 matchOnlyAtCol
函数(我想我以后可以提供一个修改版本,它接受一个范围而不是单个列)。然而,我陷入了 matchOnlyAtCol
的一些奇怪行为。这是我的演示代码:
b_only_near_end = pp.Word(pp.nums)\
.setParseAction(pp.matchOnlyAtCol(12))('B')
a = pp.ZeroOrMore(pp.Word(pp.alphanums), stopOn=b_only_near_end)('A')
c = pp.ZeroOrMore(pp.Word(pp.alphas))('C')
expr = a + pp.Optional(b_only_near_end) + c
1) 当我将第一个示例字符串 "alpha beta 123 cpart"
送入 expr
的 ParseString
时,我得到了预期的结果
A: ['alpha', 'beta']
B: '123'
C: ['cpart']
因为 B 正好从第 12 列开始。
2) 当我给它输入第二个字符串 "123 alpha beta cpart"
(第 1 列的 B 部分)我得到
ParseException:
Expected end of text (at char 0), (line:1, col:1)
">!<123 alpha beta cpart"
为什么? b_only_near_end
根本不应该匹配,因此不会停止表达式 a
,所以我希望 a
吃掉所有字符并且我不希望出现异常,因为所有部分在某种程度上是可选的(通过 Optional
class 或通过 ZeroOrMore
构造)。
更新:匹配调试揭示了什么
我通过 setDebug()
通过以下表达式代码为 ZeroOrMore
元素打开调试:
b_word = pp.Word(pp.nums).setName('_B_word_')
b_word.setDebug()
b_only_near_end = b_word\
.setParseAction(pp.matchOnlyAtCol(12))('B')
a_word = pp.Word(pp.alphanums).setName('_A_word_')
a_word.setDebug()
a = pp.ZeroOrMore(a_word, stopOn=b_only_near_end).setName('__A__')('A')
a.setDebug()
c_word = pp.Word(pp.alphas).setName('_C_word_')
c_word.setDebug()
c = pp.ZeroOrMore(c_word).setName('__C__')('C')
c.setDebug()
expr = a + pp.Optional(b_only_near_end) + c
1) 当输入字符串 "alpha beta 123 cpart"
时,我得到调试输出:
Match __A__ at loc 0(1,1)
Match _B_word_ at loc 0(1,1)
Exception raised:Expected _B_word_ (at char 0), (line:1, col:1)
Match _A_word_ at loc 0(1,1)
Matched _A_word_ -> ['alpha']
Match _B_word_ at loc 5(1,6)
Exception raised:Expected _B_word_ (at char 6), (line:1, col:7)
Match _A_word_ at loc 5(1,6)
Matched _A_word_ -> ['beta']
Match _B_word_ at loc 10(1,11)
Matched _B_word_ -> ['123']
Matched __A__ -> ['alpha', 'beta']
Match _B_word_ at loc 11(1,12)
Matched _B_word_ -> ['123']
Match __C__ at loc 14(1,15)
Match _C_word_ at loc 15(1,16)
Matched _C_word_ -> ['cpart']
Match _C_word_ at loc 20(1,21)
Exception raised:Expected _C_word_ (at char 20), (line:1, col:21)
Matched __C__ -> ['cpart']
2) 使用字符串 "123 alpha beta cpart"
输出是:
Match __A__ at loc 0(1,1)
Match _B_word_ at loc 0(1,1)
Matched _B_word_ -> ['123']
Matched __A__ -> []
Match _B_word_ at loc 0(1,1)
Exception raised:matched token not at column 12 (at char 0), (line:1, col:1)
Match __C__ at loc 0(1,1)
Match _C_word_ at loc 0(1,1)
Exception raised:Expected _C_word_ (at char 0), (line:1, col:1)
Matched __C__ -> []
加上 ParseException:
Expected end of text (at char 0), (line:1, col:1)
">!<123 alpha beta cpart"
所以这意味着部分 A 匹配字符串的开头 - 匹配结果为空,因为 a_word
不匹配 - 所以我想我必须 A比较贪心,但是如何呢?
奇怪的是
Matched __A__ -> []
发生在
之前
Match _B_word_ at loc 0(1,1)
Exception raised:matched token not at column 12 (at char 0), (line:1, col:1)
A 应该 "wait" 匹配结果更长,但我如何强制它这样做?
也许整个方法没有成效?有没有其他方法可以只在字符串的第二部分实现匹配?
1) 在 First approximation with pyparsing 的代码中查看行
b_only_near_end = pp.Word(pp.nums)\
.setParseAction(pp.matchOnlyAtCol(12))('B')
附加解析动作时,设置callDuringTry
选项:
b_only_near_end = pp.Word(pp.nums)\
.setParseAction(pp.matchOnlyAtCol(12),
callDuringTry=True))('B')
然后 matchOnlyAtCol
也将被检查 "during lookaheads and alternate testing"(引用 from the docu)。 没有这个选项就不会发生!
2) 为了解决标题问题“仅当元素出现在解析字符串的右半部分时才匹配它”(在下拼写问题) 定义一个函数:
def matchOnlyInRightHalf():
"""
Helper method for defining parse actions that require matching in the
right half of the parse string.
"""
def verifyInRightHalf(strg,locn,toks):
col = pp.col(locn,strg)
middle = len(strg) // 2
if not (col> middle):
raise pp.ParseException(strg, locn,
"matched token not in right half of string")
return verifyInRightHalf
并将其用作解析操作:
b_only_near_end = b_word.setParseAction(matchOnlyInRightHalf(),
callDuringTry=True)
问题
我想创建一个解析器来匹配像
这样的字符串"alpha beta 123 cpart"
----^----- -^- --^--
A: B: C:
alphanums num alpha
然而,B 部分仅当出现在字符串的后半部分(即字符串中点的 'right' 时)才会匹配。
所以上面的示例字符串应该被解析成以下部分:
A: ['alpha', 'beta']
B: '123'
C: ['cpart']
但是字符串"123 alpha beta cpart"
应该解析成:
A: '123 alpha beta cpart'
B: ''
C: ''
使用 pyparsing 的第一次近似
作为 pyparsing
的起点,我尝试使用 matchOnlyAtCol
函数(我想我以后可以提供一个修改版本,它接受一个范围而不是单个列)。然而,我陷入了 matchOnlyAtCol
的一些奇怪行为。这是我的演示代码:
b_only_near_end = pp.Word(pp.nums)\
.setParseAction(pp.matchOnlyAtCol(12))('B')
a = pp.ZeroOrMore(pp.Word(pp.alphanums), stopOn=b_only_near_end)('A')
c = pp.ZeroOrMore(pp.Word(pp.alphas))('C')
expr = a + pp.Optional(b_only_near_end) + c
1) 当我将第一个示例字符串 "alpha beta 123 cpart"
送入 expr
的 ParseString
时,我得到了预期的结果
A: ['alpha', 'beta']
B: '123'
C: ['cpart']
因为 B 正好从第 12 列开始。
2) 当我给它输入第二个字符串 "123 alpha beta cpart"
(第 1 列的 B 部分)我得到
ParseException:
Expected end of text (at char 0), (line:1, col:1)
">!<123 alpha beta cpart"
为什么? b_only_near_end
根本不应该匹配,因此不会停止表达式 a
,所以我希望 a
吃掉所有字符并且我不希望出现异常,因为所有部分在某种程度上是可选的(通过 Optional
class 或通过 ZeroOrMore
构造)。
更新:匹配调试揭示了什么
我通过 setDebug()
通过以下表达式代码为 ZeroOrMore
元素打开调试:
b_word = pp.Word(pp.nums).setName('_B_word_')
b_word.setDebug()
b_only_near_end = b_word\
.setParseAction(pp.matchOnlyAtCol(12))('B')
a_word = pp.Word(pp.alphanums).setName('_A_word_')
a_word.setDebug()
a = pp.ZeroOrMore(a_word, stopOn=b_only_near_end).setName('__A__')('A')
a.setDebug()
c_word = pp.Word(pp.alphas).setName('_C_word_')
c_word.setDebug()
c = pp.ZeroOrMore(c_word).setName('__C__')('C')
c.setDebug()
expr = a + pp.Optional(b_only_near_end) + c
1) 当输入字符串 "alpha beta 123 cpart"
时,我得到调试输出:
Match __A__ at loc 0(1,1)
Match _B_word_ at loc 0(1,1)
Exception raised:Expected _B_word_ (at char 0), (line:1, col:1)
Match _A_word_ at loc 0(1,1)
Matched _A_word_ -> ['alpha']
Match _B_word_ at loc 5(1,6)
Exception raised:Expected _B_word_ (at char 6), (line:1, col:7)
Match _A_word_ at loc 5(1,6)
Matched _A_word_ -> ['beta']
Match _B_word_ at loc 10(1,11)
Matched _B_word_ -> ['123']
Matched __A__ -> ['alpha', 'beta']
Match _B_word_ at loc 11(1,12)
Matched _B_word_ -> ['123']
Match __C__ at loc 14(1,15)
Match _C_word_ at loc 15(1,16)
Matched _C_word_ -> ['cpart']
Match _C_word_ at loc 20(1,21)
Exception raised:Expected _C_word_ (at char 20), (line:1, col:21)
Matched __C__ -> ['cpart']
2) 使用字符串 "123 alpha beta cpart"
输出是:
Match __A__ at loc 0(1,1)
Match _B_word_ at loc 0(1,1)
Matched _B_word_ -> ['123']
Matched __A__ -> []
Match _B_word_ at loc 0(1,1)
Exception raised:matched token not at column 12 (at char 0), (line:1, col:1)
Match __C__ at loc 0(1,1)
Match _C_word_ at loc 0(1,1)
Exception raised:Expected _C_word_ (at char 0), (line:1, col:1)
Matched __C__ -> []
加上 ParseException:
Expected end of text (at char 0), (line:1, col:1)
">!<123 alpha beta cpart"
所以这意味着部分 A 匹配字符串的开头 - 匹配结果为空,因为 a_word
不匹配 - 所以我想我必须 A比较贪心,但是如何呢?
奇怪的是
Matched __A__ -> []
发生在
之前Match _B_word_ at loc 0(1,1)
Exception raised:matched token not at column 12 (at char 0), (line:1, col:1)
A 应该 "wait" 匹配结果更长,但我如何强制它这样做?
也许整个方法没有成效?有没有其他方法可以只在字符串的第二部分实现匹配?
1) 在 First approximation with pyparsing 的代码中查看行
b_only_near_end = pp.Word(pp.nums)\
.setParseAction(pp.matchOnlyAtCol(12))('B')
附加解析动作时,设置callDuringTry
选项:
b_only_near_end = pp.Word(pp.nums)\
.setParseAction(pp.matchOnlyAtCol(12),
callDuringTry=True))('B')
然后 matchOnlyAtCol
也将被检查 "during lookaheads and alternate testing"(引用 from the docu)。 没有这个选项就不会发生!
2) 为了解决标题问题“仅当元素出现在解析字符串的右半部分时才匹配它”(在下拼写问题) 定义一个函数:
def matchOnlyInRightHalf():
"""
Helper method for defining parse actions that require matching in the
right half of the parse string.
"""
def verifyInRightHalf(strg,locn,toks):
col = pp.col(locn,strg)
middle = len(strg) // 2
if not (col> middle):
raise pp.ParseException(strg, locn,
"matched token not in right half of string")
return verifyInRightHalf
并将其用作解析操作:
b_only_near_end = b_word.setParseAction(matchOnlyInRightHalf(),
callDuringTry=True)