home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / perl / 5005 < prev    next >
Encoding:
Internet Message Format  |  1992-07-28  |  1.2 KB

  1. Path: sparky!uunet!dtix!darwin.sura.net!mips!swrinde!cs.utexas.edu!milano!paintbrush.mcc.com!barnett
  2. From: barnett@paintbrush.aca.mcc.com (Jim Barnett)
  3. Newsgroups: comp.lang.perl
  4. Subject: iterative matching
  5. Message-ID: <BARNETT.92Jul28173810@paintbrush.aca.mcc.com>
  6. Date: 28 Jul 92 23:38:10 GMT
  7. Sender: barnett@mcc.com (Jim Barnett)
  8. Organization: MCC
  9. Lines: 31
  10.  
  11.  
  12. I would like to do an iterative regexp match and find at each step the
  13. substring to the right of the match.  That is, given pattern /aa/ and
  14. string "aabbaacc", I would like to find first "bbaacc" and next "cc".
  15. But I'm having problems when I try to combine //g with $'.
  16.  
  17. More details: If I execute the following script on a file containing
  18. the single line "aaa", it prints out "a" three times on separate
  19. lines, as you would expect:
  20.  
  21. while(<>){
  22.  while(/\w/g) {
  23.    printf "%s\n", $&;
  24.  }}
  25.  
  26. But if I change $& to $', I get a segmentation fault (SunOS 4.?). 
  27. What I'd like is to print first "aa", then "a", then <empty-string>.
  28. I can add an explicit clause for the rest of the string:
  29.  
  30. while(<>){
  31.  while(/(\w)(.*)/g) {
  32.    printf "%s\n", $2;
  33.  }}
  34.  
  35. But this still doesn't do what I want because it matches only once
  36. (i.e., it prints out only "aa"). 
  37.  
  38. Any advice for a novice? 
  39.  
  40. - Jim 
  41. -- 
  42.