How to rewrite regexp without positive look-ahead?

1.1k Views Asked by At

The regex [^a-z0-9%*][a-z0-9%]{3,}(?=[^a-z0-9%*]) is not supported by Rust's default regex create due to positive look-ahead (?=):

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
regex parse error:
    [^a-z0-9%*][a-z0-9%]{3,}(?=[^a-z0-9%*])
                            ^^^
error: look-around, including look-ahead and look-behind, is not supported
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
)

What's the optimal way to rewrite it or make it working?

I've found fancy-regex crate but i'd like to avoid using both crates (or prefer fancy over default) just for one missing feature.

PS. here it is at least one expected matches example.

1

There are 1 best solutions below

7
Tim Biegeleisen On

Place your current matching pattern into a capture group, and also match (but do not capture) the term currently inside the lookahead:

([^a-z0-9%*][a-z0-9%]{3,})(?:[^a-z0-9%*]|$)