home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!dtix!darwin.sura.net!mips!swrinde!cs.utexas.edu!milano!paintbrush.mcc.com!barnett
- From: barnett@paintbrush.aca.mcc.com (Jim Barnett)
- Newsgroups: comp.lang.perl
- Subject: iterative matching
- Message-ID: <BARNETT.92Jul28173810@paintbrush.aca.mcc.com>
- Date: 28 Jul 92 23:38:10 GMT
- Sender: barnett@mcc.com (Jim Barnett)
- Organization: MCC
- Lines: 31
-
-
- 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 $'.
-
- More details: If I execute the following script on a file containing
- the single line "aaa", it prints out "a" three times on separate
- lines, as you would expect:
-
- while(<>){
- while(/\w/g) {
- printf "%s\n", $&;
- }}
-
- But if I change $& to $', I get a segmentation fault (SunOS 4.?).
- What I'd like is to print first "aa", then "a", then <empty-string>.
- I can add an explicit clause for the rest of the string:
-
- while(<>){
- while(/(\w)(.*)/g) {
- printf "%s\n", $2;
- }}
-
- But this still doesn't do what I want because it matches only once
- (i.e., it prints out only "aa").
-
- Any advice for a novice?
-
- - Jim
- --
-