Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support negative lookahead assertion. #48

Open
marcelocantos opened this issue Mar 7, 2020 · 0 comments
Open

Support negative lookahead assertion. #48

marcelocantos opened this issue Mar 7, 2020 · 0 comments
Labels
P2 Low priority

Comments

@marcelocantos
Copy link
Contributor

marcelocantos commented Mar 7, 2020

The term /{a(?!b)} matches a but only if it the next match wouldn't be b. This can be useful to avoid accidentally absorbing content that will stymie later matches. e.g.: "-"? "->" will never match -> because the first term absorbs the -. This can be fixed by not matching - when it's followed by >: /{-(?!>)}? "->".

Unfortunately, RE2 doesn't support any form of look-aside assertions. However, the above special case can be faked by matching ab|(a) and succeed only if the capturing group has something in it. This should be recursively generalisable:

a(?!b)c(?!d)e(?!f) = ab | (   ac(?!d)e(?!f) )
                   = ab | (?: acd | (   ace(?!f) ) )
                   = ab |     acd | (   ace(?!f) )
                   = ab |     acd | (?: acef | (ace) )
                   = ab |     acd |     acef | (ace)

In simple terms: eliminate failed pathways first, then match the successful pathway. If a...f themselves contain lookahead assertions, they can probably be rewritten recursively, though some thought must be given to capturing groups.

Positive lookahead, a(?=b) can also be faked, though the value proposition isn't as compelling. The technique is also different: match a(b) but, after a successful match, return the input state back to the beginning of b.

In terms of sequencing the work, supporting a single look-ahead at the end of the regexp would be a very useful MVP.

@ChloePlanet ChloePlanet added the P2 Low priority label Apr 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2 Low priority
Projects
None yet
Development

No branches or pull requests

2 participants