I too am having some trouble with Regular Expressions. For instance I ran the following regular expressions. I understand the result for (ab)* but when I switch from a greedy quantifier to a reluctant quantifier it finds nothing at each index and I am not able to understand why. Can someone please explain this to me?
Thanks, I have been referring to that tutorial but still don't understand why the second regular expression didn't find an 'ab' match. A reluctant qualifier consumes the string one character at a time but I still thought it would find an 'ab' match.
Thanks for prompting on this question. I still haven't been able to find an answer to explain this reg exp output. Either I am missing something really fundamental here or it is not the correct output for such an expression.
This is how I think it works (I've not read much about Regex so might be wrong)
Regex Engine's first priority is the overall success of the regex. Reluctant quantifiers try to match as less as possible. They'll only match more, if that can effect the overall success of the regular expression. Suppose your regex is a*?, and your input is aaaa, then it will try to match as less as possible so it'll result only in zero length matches. But if your regex is a*?b, and input is aaabaab, then you'll get aaab and aab as matches, this time a*? matched more because otherwise the whole regex would've failed. Since in the earlier case (a*? and aaaa), zero length matches still lead to the success of the regex, so regex engine didn't match anything. This zero length match thing will only happen in case of (ab)*? and not in (ab)+?, as + doesn't allow zero length matches, so regex engine will have to match 6 pairs of "ab"s (on input ababababababa)to make the regex successful. To test on my point about a*?b, you can try the regex (ab)*?b on the input abbabbabababa (note: this input is slightly different from the one in the original question)...
(PS: Henry solved my misconception about this here which made life a lot easier with regex questions for me )