How to build a negative lookahead parser in nom?

267 Views Asked by At

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

There are 1 best solutions below

1
MeetTitan On BEST ANSWER

Try using terminated()

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