[Buffalo-pm] regexp returning count of pattern.

Josh Johnson joshj at linuxmail.org
Tue Jan 17 12:42:51 PST 2006


----- 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  &nbsp;'s generated by an html editor into some 
xml for me. So "&nbsp;&nbsp;&nbsp;&nbsp;" would become 
'<spaces="4"/>' or something like that. So the problem is, I don't 
want to count *all* &nbsp;'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.

-Josh

> 
>   - Kevin
> 
> 
> On 1/17/06 12:26 PM, "Josh Johnson" <joshj at linuxmail.org> wrote:
> 
> > I need a way to get the count of a pattern being matched. I can make up an
> > elaborate function to do this but I wondered if perl had 
> something built in I
> > could use. For example, say I have the string:
> >
> > $string = 'abcdeeeeeefgheeeeijk';
> >
> > I'd like to replace it with something like:
> >
> > 'abcdE=6fghE=4ijk'
> >
> > Is there any way I can get a regexp to return the number of times that a
> > parameter was matched?
> >
> > -Josh J
> 
> --
> Kevin Eye
> Web Applications Developer
> Marketing and Creative Services
> University at Buffalo
> 330 Crofts Hall
> Buffalo, NY 14260
> eye at buffalo.edu
> phone (716) 645-5000 x1435
> fax (716) 645-3765


-- 
______________________________________________
Check out the latest SMS services @ http://www.linuxmail.org
This allows you to send and receive SMS through your mailbox.

Powered by Outblaze


More information about the Buffalo-pm mailing list