$s = 'fred';;
print for $s =~ /^(.)?(.)?(.)?(.)?(.)?(.)?$/;;
f
r
e
d
Use of uninitialized value $_ in print at (eval 10) line 1, <STDIN> li
+ne 2.
Use of uninitialized value $_ in print at (eval 10) line 1, <STDIN> li
+ne 2.
Although there are 6 capture groups, and only 4 characters, the capture groups are optional because of the trailing '?' ((.)?).
So long as the regex has more capture groups than the string contains matches, the regex will match as many as are available, and the remaining capture groups will (optionally) "match" nothing, and so return undefs.
Does that clarify things?
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
|