A regular expression usually matches the
leftmost occurrence of a pattern within an incoming (source) string.
This doesn't matter if all you're looking to do is find whether or not your source string contains something ... but if you're looking to make use of the part that matched, then it does make a difference. Consider
$us = 'I am graham@wellho.net and you are pinkpanther@frenchdetectives.fr';
and the match
/\S+\@\S+/
This will match
graham@wellho.net every time you run it in Perl, and never
pinkpanther@frenchdetectives.fr .
What if you want to match that second email address, then? You can add a
modifier after the end of the regular expression - the single letter
g which stands for "global".
* In a scalar content, but used in a loop, a global match
carries on where the last match left off on each successive time around the loop, thus letting you loop though all valid, non-overlapping matches in the string. And when there are no more, a false result will be returned. Thus:
while ($us =~ /\S+\@\S+/g) {
print "Emma is $&\n";
}
Will return each match it turn. By contrast, without the
g this program would give you an infinite loop.
There are, as always, multiple ways of doing the same thing in Perl. If you use the
g modifier in a list context (for example return the result of the match into a list), that list will be assigned to all non-overlapping matches. Thus:
@gotted = $us =~ /\S+\@\S+/g;
print "We got @gotted\n";
will output
We got graham@wellho.net pinkpanther@frenchdetectives.fr
Complete program
[here]. As taught during our
Learning to program in Perl / Perl Programming training classes.
(written 2011-12-09, updated 2011-12-17)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
21ae
G206 - Well House Consultants - Our training centre [4012] A course is not just for a year - its for a career - (2013-02-20)
[2943] Our facilities to support Well House Consultants Courses - (2010-09-02)
[2926] Journey times to Melksham, Wiltshire - (2010-08-17)
[2660] One number for Well House - 01225 708225 - (2010-03-04)
[2538] Open Source Training Centre and Courses for 2010 - (2009-12-16)
[2537] Faster network, but not faster browsing until ... - (2009-12-14)
[2126] Weeding out old phone numbers - (2009-04-11)
[1240] Fancy going to Glastonbury? - (2007-06-22)
[1200] Training information - England, Scotland, Wales and Ireland - (2007-05-22)
[640] Training Centre Pictures - (2006-03-09)
[627] JIT or JAU - (2006-02-27)
[448] Out of the norm. - (2005-09-23)
[256] Spring is in the air - (2005-03-24)
[53] Drive the drive - (2004-09-18)
Some other Articles
Well House Manor - perhaps the best hotel rooms in MelkshamDark mornings, dog update, and Python and Lua courses before ChristmasUsing Perl to generate multiple reports from a HUGE file, efficientlyThe difference between dot (a.k.a. full stop, period) and comma in PerlFinding all matches to a pattern in Perl regular expressionsLooking for hotel rooms in Melksham over Christmas? We still have some availabilitySome different pictures from MelkshamWhat order are operations performed in, in a Perl expression?I loves Melksham Easy session example in PHP - keeping each customers data apart