|Don't ask to ask, just ask|
\1 Greed, $1by kryten (Scribe)
|on Apr 10, 2000 at 14:55 UTC ( #7225=note: print w/replies, xml )||Need Help??|
1. Why the (xx+) part does not match the entire string.
Used on its own, it would. The thing is that when it is combined with \1+ in the regex: /(xx+)\1+/, it is now requred to match 2 or more x's followed by 1 or more of the same number of x's again. Which just doesn't work if (xx+) gobbles all the x's to start with! It still is greedy, in that 6x's will match as xxx and xxx rather than xx and 2 lots of xx, but it can't be TOO greedy or the match doesn't work.
2. Why $1 is not exactly the same as \1
As with most things in Perl, these two are kinda the same but not identical. $1 is actually what was matched in the LAST pattern match and \1 is a backreference which only works within the CURRENT pattern match. If you use \1 outside of a regexp you are likely to just get a reference to a constant '1'. Gramatical errors aside, see if this code makes anything clearer.
Note that in this case you could use $1 rather than \1 in the first and third lines but not in the second. And that you cannot use \1 instead of $1 in the fourth.
An interesting? variaton.
By removing the negation and printing out what matched (this time using $1) you can get the largest factor of a number. (or that many x's anyway) )
Or a half a christmas tree. :)
In Section Craft