home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / perl / 5907 < prev    next >
Encoding:
Text File  |  1992-09-13  |  1.6 KB  |  48 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!convex!convex!tchrist
  3. From: Tom Christiansen <tchrist@convex.COM>
  4. Subject: Re: Regexp question/suggestion
  5. Originator: tchrist@pixel.convex.com
  6. Sender: usenet@news.eng.convex.com (news access account)
  7. Message-ID: <1992Sep13.223221.3292@news.eng.convex.com>
  8. Date: Sun, 13 Sep 1992 22:32:21 GMT
  9. Distribution: comp
  10. Reply-To: tchrist@convex.COM (Tom Christiansen)
  11. References: <KRAB.92Sep13154332@xiv.iesd.auc.dk>
  12. Nntp-Posting-Host: pixel.convex.com
  13. Organization: Convex Computer Corporation, Colorado Springs, CO
  14. X-Disclaimer: This message was written by a user at CONVEX Computer
  15.               Corp. The opinions expressed are those of the user and
  16.               not necessarily those of CONVEX.
  17. Lines: 29
  18.  
  19. From the keyboard of krab@iesd.auc.dk (Kresten Krab Thorup):
  20. :How do I find the number of matches for regexp `R' in a string `S' ?
  21. :My current solution is this:
  22. :
  23. :    $string = 'S';
  24. :    $mathces = ($string =~ s/R/\&/g);
  25. :
  26. :Now, what is the reason for the fact, that regexp-substitusion has the
  27. :value of the number of the matches, while ordinary regexp-match
  28. :hasn't?  To me, it would seem resonable, to have an extra flag, say
  29. :`n' for regular expression matches, so that one could say:
  30. :
  31. :    $matches = ($string =~ /R/n);
  32.  
  33. We've got one of those:
  34.  
  35.     $matches++ while $string =~ /R/g;
  36.  
  37. or if you have ( ) subexpressions that you care about:
  38.  
  39.     $matches = @substrs = $string =~ /(R)/g;
  40.  
  41.  
  42. --tom
  43. -- 
  44.     Tom Christiansen      tchrist@convex.com      convex!tchrist
  45.  
  46. Pascal, n.: A programming language named after a man who would turn over in
  47. his grave if he knew about it.
  48.