287c Finding all matches to a pattern in Perl regular expressions
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Finding all matches to a pattern in Perl regular expressions

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)


Back to
Looking for hotel rooms in Melksham over Christmas? We still have some availability
Previous and next
or
Horse's mouth home
Forward to
The difference between dot (a.k.a. full stop, period) and comma in Perl
Some other Articles
Well House Manor - perhaps the best hotel rooms in Melksham
Dark mornings, dog update, and Python and Lua courses before Christmas
Using Perl to generate multiple reports from a HUGE file, efficiently
The difference between dot (a.k.a. full stop, period) and comma in Perl
Finding all matches to a pattern in Perl regular expressions
Looking for hotel rooms in Melksham over Christmas? We still have some availability
Some different pictures from Melksham
What order are operations performed in, in a Perl expression?
I loves Melksham
Easy session example in PHP - keeping each customers data apart
4106 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page
3df9

© WELL HOUSE CONSULTANTS LTD., 2013: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3545_Fin ... sions.html • PAGE BUILT: Mon May 27 06:13:46 2013 • BUILD SYSTEM: wizard
0