英文:
How to build a negative lookahead parser in nom?
问题
fn parser(input: &str) -> IResult<&str, &str> {
    cond(peek(not(tag(" world"))), tag("hello"))(input)
}
英文:
How can I create a negative lookahead parser for nom?
For example, I'd like to parse "hello", except if it's followed by " world". The equivalent regex would be hello(?! world).
I tried to combine the cond, not and peek parsers
fn parser(input: &str) -> IResult<&str, &str> {
    cond(peek(not(tag(" world"))(input)), tag("hello"))(input)
}
but this doesn't work as cond expects the condition as bool instead of as IResult.
答案1
得分: 2
Try using terminated()
terminated(tag("hello"), not(tag(" world")))
				通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论