我编写了一个快速的attoparsec解析器,以遍历aspx文件并删除所有样式属性,并且工作正常,除了其中一个片段,在其中我无法弄清楚如何在不消耗>
的情况下使它成功匹配。
这是我所拥有的:
anyTill = manyTill anyChar
anyBetween start end = start *> anyTill end
styleWithQuotes = anyBetween (stringCI "style=\"") (stringCI "\"")
styleWithoutQuotes = anyBetween (stringCI "style=") (stringCI " " <|> ">")
everythingButStyles = manyTill anyChar (styleWithQuotes <|> styleWithoutQuotes) <|> many1 anyChar
我了解部分原因是我在everythingButStyles中使用manageTill的方式,这就是我积极将所有样式的东西放到地上的方法,但是在
styleWithoutQuotes
中,我需要它匹配“>”作为结尾,但不使用它,在parsec我本来可以完成lookAhead ">"
,但是我不能在attoparsec中做到这一点。 最佳答案
同时, lookAhead
组合器已添加到attoparsec中,因此现在可以仅使用lookAhead (char '>')
或lookAhead (string ">")
来实现此目标。
以下是引入之前的解决方法。
您可以使用 peekWord8
构建非使用的解析器,该解析器仅查看下一个字节(如果有)。由于ByteString
具有Monoid
实例,因此Parser ByteString
是MonadPlus
,您可以使用
lookGreater = do
mbw <- peekWord8
case mbw of
Just 62 -> return ">"
_ -> mzero
(62是
'>'
的代码点)以找到不消耗它的'>'
或失败。