[Buffalo-pm] regexp returning count of pattern.
John Macdonald
john at perlwolf.com
Tue Jan 17 14:32:10 PST 2006
On Tue, Jan 17, 2006 at 03:42:51PM -0500, Josh Johnson wrote:
>
> ----- Original Message -----
> From: "Kevin Eye" <eye at buffalo.edu>
> To: "Josh Johnson" <joshj at linuxmail.org>, buffalo-pm at mail.pm.org
> Subject: Re: [Buffalo-pm] regexp returning count of pattern.
> Date: Tue, 17 Jan 2006 12:30:41 -0500
>
> >
> > The /g flag on regex substitutions returns the number of substitutions.
> >
> > Try this:
> >
> > $text = 'abcba';
> > $num_substitutions = $text =~ /b/d/g;
> >
> > $num_substitutions is 2 because two b's were replaced with d's.
> >
> > You can do this without substitutions to count the number of matches, too.
>
> I can only get this to work with substitution. How can I do it
> without? Its not a big deal, I can work around it if needbe. But
> now I'm running into another problem: How can I match a *string*
> one or more times? I thought I'd start out with baby-steps with
> this. But that's not gonna happen. Here's my ultimate goal:
>
> To convert multiple 's generated by an html editor into some
> xml for me. So " " would become
> '<spaces="4"/>' or something like that. So the problem is, I don't
> want to count *all* 's, I just want to count the ones that
> are in sequence.
>
> John: I tried your method. I see where you are going with it, and it seems like it should work, but I can't get it to.
>
> If I could match one or more strings I could get something to work. But I only know how to get regex's to match one or more characters.
$string =~ s/((\ ){2,})/'<spaces="'.(length($2)/6).'/>'/eg;
(This gets harder if there are extra characters allowed
within the run, though.)
--
More information about the Buffalo-pm
mailing list