home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- 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
- From: rantapaa@s6.math.umn.edu (Erik E. Rantapaa)
- Subject: Re: iterative matching
- Message-ID: <rantapaa.712388573@s6.math.umn.edu>
- Sender: news@news2.cis.umn.edu (Usenet News Administration)
- Nntp-Posting-Host: s6.math.umn.edu
- Organization: University of Minnesota
- References: <BARNETT.92Jul28173810@paintbrush.aca.mcc.com>
- Date: Wed, 29 Jul 1992 05:42:53 GMT
- Lines: 71
-
- barnett@paintbrush.aca.mcc.com (Jim Barnett) writes:
-
- >I would like to do an iterative regexp match and find at each step the
- >substring to the right of the match. That is, given pattern /aa/ and
- >string "aabbaacc", I would like to find first "bbaacc" and next "cc".
- >But I'm having problems when I try to combine //g with $'.
-
- I too would like to see $' and $` working with //g. Here's a case
- where it would come in handy. It also shows you one way to get around
- the problem -- namely use s///eg:
-
- # pull_quotes -- Erik Rantapaa, rantapaa@math.umn.edu
- #
- # Based on the original version by Tom Christiansen.
- # Only pulls the top level of quotes.
-
- sub pull_quotes {
- local($_, $qchars) = @_;
-
- local($qL, $qR); # left and right quote characters
- local($level); # current quote level
- local(@startstop); # list of start and stopping locations
-
- die "need two just quote chars" if length($qchars) != 2;
-
- $qL = substr($qchars, 0, 1);
- $qR = substr($qchars, 1, 1);
-
- ###
- ### Note: while (/[$qchars]/g) { &startstop; } doesn't work here
- ### because $` is not set properly.
- ###
- s/[$qchars]/&startstop/eg;
- while (@startstop) {
- ($start, $stop) = splice(@startstop, $[, 2);
- push(@quotes, substr($_, $start+1, $stop-$start-1));
- }
- @quotes;
- }
-
- sub startstop {
- if ($& eq $qL) {
- push(@startstop, length($`)) if $level == 0;
- $level++;
- } else {
- if ($level > 0) {
- $level--;
- push(@startstop, length($`)) if $level == 0;
- } else {
- die "Wha happened????";
- }
- }
- $&;
- }
-
- Another option is to use:
-
- while (/\w/) {
- ... do stuff here ...
- $_ = substr($_, $[+length($`)+length($&));
- }
-
- This eats up $_, but that's probably not a big deal. Also, it differs
- from using s///eg in that $` is the stuff *between* matches, whereas
- $` in a s///eg statement is the prefix of the match starting from the
- beginning of the string.
- --
- Erik Rantapaa rantapaa@math.umn.edu
-
-
- God is not dead! He's alive and autographing bibles at Cody's
-