Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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" )))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading