in reply to can't use string refs while "strict refs"
I would say that not everything that you read in the tutorials is common - there are some very complicated and rare things - and this is one of them!
This is so rare, that I don't think that it is worth delving into the mind-numbing details. VERY seldom do you even care in the slightest about the pos(position) of a match - that is 'C' thinking.
Perl regex is most commonly used to capture stuff that is between other stuff and whether this happens at index 5 or 95 usually just doesn't make any difference at all. The purists would say: "well sometimes it does matter" and I would agree with them. What I am saying here is that the pos() almost always does not matter.
I show some more common ideas in Perl below.
#!usr/bin/perl use warnings; use strict; my $x = "Mmm...donut, thought... Homer...Yech...peas"; my @matches = $x =~ /(Mmm|Yech)\.\.\.(donut|peas)/g; print "number of matches is: ".@matches."\n"; print "matches are: @matches\n"; print "\n"; $x = "MmmABCdonut, thought... HomerABXYechABYpeas"; my @moreMatches = $x =~ /(?:Mmm|Yech)(...)(?:donut|peas)/g; print "moreMatches number: ".@moreMatches,"\n"; print "@moreMatches\n"; __END__ prints: number of matches is: 4 matches are: Mmm donut Yech peas moreMatches number: 2 ABC ABY
|
|---|