home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / perl / 5900 < prev    next >
Encoding:
Internet Message Format  |  1992-09-13  |  1018 b 

  1. Path: sparky!uunet!math.fu-berlin.de!fauern!Germany.EU.net!mcsun!sunic!dkuug!iesd!iesd.auc.dk!krab
  2. From: krab@iesd.auc.dk (Kresten Krab Thorup)
  3. Newsgroups: comp.lang.perl
  4. Subject: Regexp question/suggestion
  5. Message-ID: <KRAB.92Sep13154332@xiv.iesd.auc.dk>
  6. Date: 13 Sep 92 14:43:32 GMT
  7. Sender: news@iesd.auc.dk (UseNet News)
  8. Distribution: comp
  9. Organization: Mathematics and Computer Science, Aalborg University
  10. Lines: 18
  11.  
  12. How do I find the number of matches for regexp `R' in a string `S' ?
  13. My current solution is this:
  14.  
  15.     $string = 'S';
  16.     $mathces = ($string =~ s/R/\&/g);
  17.  
  18. Now, what is the reason for the fact, that regexp-substitusion has the
  19. value of the number of the matches, while ordinary regexp-match
  20. hasn't?  To me, it would seem resonable, to have an extra flag, say
  21. `n' for regular expression matches, so that one could say:
  22.  
  23.     $matches = ($string =~ /R/n);
  24.  
  25. With $1, $2... set to the values of the LAST match.  This would be
  26. CONSIDERABLY faster than the memory consuming substitution, wouldn't
  27. it? 
  28.  
  29. /Kresten
  30.