我想要解析
'This,is,an,example,text'就像在findTokens中
'This,is,an,example,text' findTokens: $,
an OrderedCollection('This' 'is' 'an' 'example' 'text')但是我不知道如何使用PetitParser,delimitedBy:和separatedBy:没有帮助我试过了
( #any asParser delimitedBy: $, asParser ) plus flatten parse: 'This,is,an,example,text'但显然不起作用
发布于 2011-10-18 22:42:51
当我想要排除某些东西时,我总是在PetitParser中使用这种模式。只需将"what I'm looking for“或"what I want to exclude”(无论哪个更容易描述)定义为解析器,然后否定它,并根据需要进行处理。
s := 'This,is,an,example,text'.
separator := $, asParser ==> [ :n | nil ].
token := separator negate plus flatten.
p := (token separatedBy: separator) ==> [ :nodes |
nodes copyWithout: nil ].
p parse: s.发布于 2012-09-04 17:24:13
您可以将delimitedBy:与withoutSeparators结合使用
|text parser|
text := 'This,is,an,example,text'.
parser := (#word asParser plus flatten delimitedBy: ($, asParser)) withoutSeparators.
parser parse: text似乎是对PetitParser的最新改进。
发布于 2011-10-18 17:03:20
a #delimitedBy: b扩展到了a , (b , a) star,所以你的解析器会说“给我一个逗号分隔的字符”。
它的可读性不是很好,但它可以满足您的需要:
((($, asParser not , #any asParser) ==> [:nodes | nodes second])
plus flatten delimitedBy: $, asParser第一个子句说“解析任何不是逗号的东西”。因此,给定'12,24',您将获得#('12' $, '24')。
https://stackoverflow.com/questions/7803675
复制相似问题