1

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?

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

This is correct. There is no "other direction" - regex always returns the earliest (leftmost) successful match. Thus, if both XaaaXaaaXaacXaaaX and XaaaXaaaXaacXaaaX match, you will always get the first one. The only way to return the second is to make sure the first one is unsuccessful.
A negated character class won't work for multicharacter values.
Yes, there is such a question on SO already. Yours is a dupe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.