使用 pyparsing 解析键值对,其中值可以在多行中继续
Parse key-value pairs with pyparsing where values may continue over multiple lines
如何用 pyparsing
解析键-属性 定义?
some_key a b c d
other_key /some/path /some/other/path.pl \
'$SOME_ENV_VAR$' \
'$OTHER_ENV_VAR$'
此示例中的键是 some_key
和 other_key
。值是行的其余部分,包括键名称后面的空格后的 [ \t]
,以及使用 \
.
进行行继续的选项
我对 pyparsing
比较陌生,这超出了我目前的能力。
source code for pyparsing
(e.g. for cppStyleComment
) 很有启发性,以下现在对我有用:
name = pp.Word(pp.alphas + '_', pp.alphanums + '_')
value = pp.Regex(r'(?:[^\n]*\\n)*[^\n]*')
definition = name + value
如何用 pyparsing
解析键-属性 定义?
some_key a b c d
other_key /some/path /some/other/path.pl \
'$SOME_ENV_VAR$' \
'$OTHER_ENV_VAR$'
此示例中的键是 some_key
和 other_key
。值是行的其余部分,包括键名称后面的空格后的 [ \t]
,以及使用 \
.
我对 pyparsing
比较陌生,这超出了我目前的能力。
source code for pyparsing
(e.g. for cppStyleComment
) 很有启发性,以下现在对我有用:
name = pp.Word(pp.alphas + '_', pp.alphanums + '_')
value = pp.Regex(r'(?:[^\n]*\\n)*[^\n]*')
definition = name + value