Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I've been looking into regex lately and figured that the ? operator makes the *,+, or ? lazy. My question is how does it do that?

Is it that *? for example is a special operator, or does the ? have an effect on the *? In other words, does regex recognize *? as one operator in itself, or does regex recognize *? as the two separate operators * and ??

If it is the case that *? is being recognized as two separate operators, how does the ? affect the * to make it lazy. If ? means that the * is optional, shouldn't this mean that the * doesn't have to exists at all. If so, then in a statement .*? wouldn't regex just match separate letters and the whole string instead of the shorter string?

Please explain, I'm desperate to understand.

share|improve this question

closed as unclear what you're asking by Martijn Pieters, BЈовић, GlenH7, Dan Pichelman, Dynamic Jul 2 '13 at 19:27

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

    
@Martijn Pieters In the practice of programming, no, but in being knowledgeable in programming, yes. –  Uriel Katz Jul 1 '13 at 8:44
    
They are two separate operators, and the ? has 2 meanings, depending on its context, same as with ^ that may mean beginning-of-line or negation if it is the first char inside a [] pair. –  Carlos Campderrós Jul 1 '13 at 9:02

1 Answer 1

up vote 5 down vote accepted

The *? is definitely one operator and not the normal * with a modifying meta-operator. It is commonly named similar to * for mnemonic purposes only.

The effect that *? has in contrast to * is subtle, but it goes deep enough into the expression engine that it has to be treated in a different way at the "transmission" level (deciding whether to backtrack or not), and that can't be done by something applied to the result of a normal *. (For much, much more information, read chapter 4 of Mastering Regular Expressions.)

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.