昨天我发了一个类似的问题:
Python Regex Named Groups。
这项工作很适合做简单的事情。
经过一些研究,我已经阅读了pyparsing库,它似乎非常适合我的任务。
text = '[@a eee, fff fff, ggg @b eee, fff, ggg @c eee eee, fff fff,ggg ggg@d]'
command_s = Suppress(Optional('[') + Literal('@'))
command_e = Suppress(Literal('@') | Literal(']'))
task = Word(alphas)
arguments = ZeroOrMore(
Word(alphas) +
Suppress(
Optional(Literal(',') + White()) | Optional(White() + Literal('@'))
)
)
command = Group(OneOrMore(command_s + task + arguments + command_e))
print command.parseString(text)
# which outputs only the first @a sequence
# [['a', 'eee', 'fff', 'fff', 'ggg']]
# the structure should be someting like:
[
['a', 'eee', 'fff fff', 'ggg'],
['b', 'eee', 'fff', 'ggg'],
['c', 'eee eee', 'fff fff', 'ggg ggg'],
['d']
]
@指示序列的开头,第一个单词是一个任务(a),后跟可选的逗号分隔参数(eee、fff fff、ggg)。问题是,上面的代码忽略了@b、@c和@d。同样“fff-fff”被看作是两个独立的参数,它应该只有一个。
最佳答案
请参阅嵌入的注释。
text = '[@a eee, fff fff, ggg @b eee, fff, ggg @c eee eee, fff fff,ggg ggg@d]'
from pyparsing import *
LBRACK,RBRACK,AT = map(Suppress,"[]@")
key = AT + Word(alphas)
# use originalTextFor to preserve whitespace between words between commas
list_item = originalTextFor(OneOrMore(Word(alphas)))
# define a key_value pair using Group to preserve structure
key_value = Group(key + Optional(delimitedList(list_item)))
parser = LBRACK + OneOrMore(key_value) + RBRACK
print parser.parseString(text)
这将打印您想要的输出。
[['a', 'eee', 'fff fff', 'ggg'],
['b', 'eee', 'fff', 'ggg'],
['c', 'eee eee', 'fff fff', 'ggg ggg'],
['d']]
对于额外的积分,下面是如何让pyparsing为您定义密钥:
# Extra credit:
# use Dict to auto-define named groups using each '@x' as a key
parser = LBRACK + Dict(OneOrMore(key_value)) + RBRACK
result = parser.parseString(text)
# print the parsed keys
print result.keys()
# print a value for a particular key
print result['c']
# print a value for a particular key using object notation
print result.b
# dump out the whole structure to see just what we got
print result.dump()
印刷品
['a', 'c', 'b', 'd']
['eee eee', 'fff fff', 'ggg ggg']
['eee', 'fff', 'ggg']
[['a', 'eee', 'fff fff', 'ggg'], ['b', 'eee', 'fff', 'ggg'], ['c', 'eee eee', 'fff fff', 'ggg ggg'], ['d']]
- a: ['eee', 'fff fff', 'ggg']
- b: ['eee', 'fff', 'ggg']
- c: ['eee eee', 'fff fff', 'ggg ggg']
- d:
关于python - 重复图案的解析顺序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14125355/