question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Negative Lookahead

See original GitHub issue

Hi, 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:closed
  • Created 6 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
wavebeemcommented, Apr 25, 2017

I’ll add this under the name notFollowedBy to emphasize that it’s a lookahead operation.

0reactions
wavebeemcommented, Apr 22, 2017

I totally missed your comment @anko. That looks like a good feature to add.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found