home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / perl / 5013 < prev    next >
Encoding:
Text File  |  1992-07-28  |  2.5 KB  |  84 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!cs.utexas.edu!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!The-Star.honeywell.com!umn.edu!s6.math.umn.edu!rantapaa
  3. From: rantapaa@s6.math.umn.edu (Erik E. Rantapaa)
  4. Subject: Re: iterative matching
  5. Message-ID: <rantapaa.712388573@s6.math.umn.edu>
  6. Sender: news@news2.cis.umn.edu (Usenet News Administration)
  7. Nntp-Posting-Host: s6.math.umn.edu
  8. Organization: University of Minnesota
  9. References: <BARNETT.92Jul28173810@paintbrush.aca.mcc.com>
  10. Date: Wed, 29 Jul 1992 05:42:53 GMT
  11. Lines: 71
  12.  
  13. barnett@paintbrush.aca.mcc.com (Jim Barnett) writes:
  14.  
  15. >I would like to do an iterative regexp match and find at each step the
  16. >substring to the right of the match.  That is, given pattern /aa/ and
  17. >string "aabbaacc", I would like to find first "bbaacc" and next "cc".
  18. >But I'm having problems when I try to combine //g with $'.
  19.  
  20. I too would like to see $' and $` working with //g.  Here's a case
  21. where it would come in handy.  It also shows you one way to get around
  22. the problem -- namely use s///eg:
  23.  
  24. # pull_quotes -- Erik Rantapaa, rantapaa@math.umn.edu
  25. #
  26. # Based on the original version by Tom Christiansen.
  27. # Only pulls the top level of quotes.
  28.  
  29. sub pull_quotes {
  30.     local($_, $qchars) = @_;
  31.  
  32.     local($qL, $qR);        # left and right quote characters
  33.     local($level);        # current quote level
  34.     local(@startstop);        # list of start and stopping locations
  35.  
  36.     die "need two just quote chars" if length($qchars) != 2;
  37.  
  38.     $qL = substr($qchars, 0, 1);
  39.     $qR = substr($qchars, 1, 1);
  40.  
  41. ###
  42. ### Note: while (/[$qchars]/g) { &startstop; } doesn't work here
  43. ### because $` is not set properly.
  44. ###
  45.     s/[$qchars]/&startstop/eg;
  46.     while (@startstop) {
  47.     ($start, $stop) = splice(@startstop, $[, 2);
  48.     push(@quotes, substr($_, $start+1, $stop-$start-1));
  49.     }
  50.     @quotes;
  51. }
  52.  
  53. sub startstop {
  54.     if ($& eq $qL) {
  55.     push(@startstop, length($`)) if $level == 0;
  56.     $level++;
  57.     } else {
  58.     if ($level > 0) {
  59.         $level--;
  60.         push(@startstop, length($`)) if $level == 0;
  61.     } else {
  62.         die "Wha happened????";
  63.     }
  64.     }
  65.     $&;
  66. }
  67.  
  68. Another option is to use:
  69.  
  70. while (/\w/) {
  71.   ... do stuff here ...
  72.   $_ = substr($_, $[+length($`)+length($&));
  73. }
  74.  
  75. This eats up $_, but that's probably not a big deal.  Also, it differs
  76. from using s///eg in that $` is the stuff *between* matches, whereas
  77. $` in a s///eg statement is the prefix of the match starting from the
  78. beginning of the string.
  79. --
  80.         Erik Rantapaa                   rantapaa@math.umn.edu
  81.  
  82.  
  83.  God is not dead!  He's alive and autographing bibles at Cody's
  84.