home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!convex!convex!tchrist
- From: Tom Christiansen <tchrist@convex.COM>
- Subject: Re: Regexp question/suggestion
- Originator: tchrist@pixel.convex.com
- Sender: usenet@news.eng.convex.com (news access account)
- Message-ID: <1992Sep13.223221.3292@news.eng.convex.com>
- Date: Sun, 13 Sep 1992 22:32:21 GMT
- Distribution: comp
- Reply-To: tchrist@convex.COM (Tom Christiansen)
- References: <KRAB.92Sep13154332@xiv.iesd.auc.dk>
- Nntp-Posting-Host: pixel.convex.com
- Organization: Convex Computer Corporation, Colorado Springs, CO
- X-Disclaimer: This message was written by a user at CONVEX Computer
- Corp. The opinions expressed are those of the user and
- not necessarily those of CONVEX.
- Lines: 29
-
- From the keyboard of krab@iesd.auc.dk (Kresten Krab Thorup):
- :How do I find the number of matches for regexp `R' in a string `S' ?
- :My current solution is this:
- :
- : $string = 'S';
- : $mathces = ($string =~ s/R/\&/g);
- :
- :Now, what is the reason for the fact, that regexp-substitusion has the
- :value of the number of the matches, while ordinary regexp-match
- :hasn't? To me, it would seem resonable, to have an extra flag, say
- :`n' for regular expression matches, so that one could say:
- :
- : $matches = ($string =~ /R/n);
-
- We've got one of those:
-
- $matches++ while $string =~ /R/g;
-
- or if you have ( ) subexpressions that you care about:
-
- $matches = @substrs = $string =~ /(R)/g;
-
-
- --tom
- --
- Tom Christiansen tchrist@convex.com convex!tchrist
-
- Pascal, n.: A programming language named after a man who would turn over in
- his grave if he knew about it.
-