Negative Lookahead
See original GitHub issueHi, is there an idiomatic way to write a parser that only matches if it is not followed by another parser?
const parseFoo = Parsimmon.string("foo");
const parseBar = Parsimmon.string("bar");
const parseFooNotFollowedByBar = ?;
parseFoo.then(parseBar).parse("foobar"); // should succeed
parseFooNotFollowedByBar.then(parseBar).parse("foobar"); // should fail
I suspect that lookahead could help here, but the documentation does not really help me:
Parses using parser, but does not consume what it parses. Yields an empty string.
So it does not change the input and always returns the same thing. What is the difference to () => ""
?
I do know that the example could easily be rewritten in an LL1 grammar that would be more efficient to parse. I’m just wondering whether parsimmon supports the approach of not matching if something else succeeds.
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Lookahead and Lookbehind Zero-Length Assertions
Negative lookahead is indispensable if you want to match something not followed by something else. When explaining character classes, this tutorial ...
Read more >Regular expression negative lookahead - regex - Stack Overflow
A negative lookahead says, at this position, the following regex can not match. Let's take a simplified example:
Read more >Positive & Negative Lookahead with Examples - Regex Tutorial
Negative Lookahead : ... In this type of lookahead the regex engine searches for a particular element which may be a character or...
Read more >Lookahead and lookbehind - The Modern JavaScript Tutorial
Negative lookahead. Let's say that we want a quantity instead, not a price from the same string. That's a number \d+ , NOT...
Read more >Lookahead and Lookbehind Tutorial—Tips &Tricks - RexEgg
This regex is what's known as a "zero-width match" because it matches a position without matching any actual characters. How does it work?...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ll add this under the name
notFollowedBy
to emphasize that it’s a lookahead operation.I totally missed your comment @anko. That looks like a good feature to add.