[Buffalo-pm] regexp returning count of pattern.

Josh Johnson joshj at linuxmail.org
Tue Jan 17 13:31:10 PST 2006


That is beautiful and works quite well. Thanks Kevin!

-Josh


----- 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 16:10:08 -0500

> 
> Looks like I lied in my first answer. According to the perlop man page,
> pattern matching (not substitution) with or without the /g flag in scalar
> context returns whether there is a match or not; not how many. In list
> context, it returns each match with the /g flag, so you can use that to get
> how many if you count the number of items in the list like this:
> 
> @matches = $text =~ /regexp/g;
> $num_matches = @matches;
> 
> So, your whole thing could be done like this:
> 
> my $text = 'abc  def';
> $text =~ s{((?: )+)}{
>      my @matches = $1 =~ / /g;
>      my $num_spaces = @matches;
>      qq{<spaces num="$num_spaces" />}
>      }eg;
> 
> 
>   - Kevin
> 
> 
> 
> On 1/17/06 3:42 PM, "Josh Johnson" <joshj at linuxmail.org> 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.
> >
> > -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
> >
> 
> --
> 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