Dotan
Dotan

Reputation: 7622

regex reverse lazy quantifier

say I have this string

X,,,X,,,X,,c,,X,,,X

and I want to catch the smallest string that matches X.*c.*X, which is X,,,X,,,X,,c,,X,,,X

the regex X.*c.*X will catch X,,,X,,,X,,c,,X,,,X

by making the second quantifier lazy X.*c.*?X I get X,,,X,,,X,,c,,X,,,X

but making the first quantifier lazy makes no difference X.*?c.*?X --> X,,,X,,,X,,c,,X,,,X

How can I tell the first quantifier to also be lazy, but from the other direction?

Upvotes: 1

Views: 395

Answers (1)

Dotan
Dotan

Reputation: 7622

explicitly disallow repetition of the starting substring.

In the example:

X[^X]*c.*?X --> XaaaXaaaXaacXaaaX

The multicharachter version:

(using negative look-around)

look for minimal XY.*c.*XY in string: XY,,,XY,,,XY,,c,,XY,,,XY

XY((?!XY).)*c.*?XY --> XY,,,XY,,,XY,,c,,XY,,,XY

Upvotes: 2

Related Questions