How to build a negative lookahead parser in nom?

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.

>Solution :

Try using terminated()

terminated(tag("hello"), not(tag(" world" )))

Leave a Reply