3

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.

4ntoine
  • 19,816
  • 21
  • 96
  • 220

1 Answers1

3

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%*]|$)
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • can you please explain ".. and also match (but do not capture) .."? Eventually i need to extract all matches from the string, eg. from `^asdf^1234^56as^` i need to extract 3 matches (`^asdf`, `^1234` and `^56as`) – 4ntoine Feb 09 '21 at 06:10
  • It works a bit different from original one for `^asdf^1234^56as^` for example. The original one finds 3 matches (`^asdf`, `^1234` and `^56as`) and the new one only 2 matches (`^asdf` and `^56as`), so the idea seems to be right, but not fully equal – 4ntoine Feb 09 '21 at 06:24
  • I think your pattern is wrong. Edit your question and include some data. – Tim Biegeleisen Feb 09 '21 at 06:29
  • the original https://regex101.com/r/6j4EZk/1 and the new one https://regex101.com/r/yqTvJf/1. I do have the tests but i need to refine them. I got the idea and i think if we fix the difference between the links it will be enough for me. Thanks – 4ntoine Feb 09 '21 at 06:34
  • @4ntoine: You cannot get 3 matches without using lookahead – anubhava Feb 09 '21 at 06:38
  • @anubhava what would be your recommendation? Currently i'm thinking about implementing it programmatically (eg. find the matches and check the look-ahead part programmatically) – 4ntoine Feb 09 '21 at 08:50
  • I recommend making this a complete question by including sample data and then explaining the logic for what you want to match. – Tim Biegeleisen Feb 09 '21 at 08:51