如何在nom中构建一个负向预查解析器?

huangapple go评论57阅读模式
英文:

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: &amp;str) -&gt; IResult&lt;&amp;str, &amp;str&gt; {
    cond(peek(not(tag(&quot; world&quot;))(input)), tag(&quot;hello&quot;))(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")))
英文:

Try using terminated()

terminated(tag(&quot;hello&quot;), not(tag(&quot; world&quot;)))

huangapple
  • 本文由 发表于 2023年2月7日 02:09:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365067.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定