Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > (newbie) How do I group a string for repetition count? {m,n}

Reply
Thread Tools

(newbie) How do I group a string for repetition count? {m,n}

 
 
Ray
Guest
Posts: n/a
 
      03-01-2004
Hello,

I'm trying the following to get the 2nd, and only the 2nd occurrence
of the phrase "turkey shoot", in a string with a lot more than 2
occurrences of the phrase:

(turkey shoot){2}

It does't match anything.

How do I structure this properly?

Thanks.
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      03-01-2004

http://www.velocityreviews.com/forums/(E-Mail Removed) (Ray) wrote:
> I'm trying the following to get the 2nd, and only the 2nd occurrence
> of the phrase "turkey shoot", in a string with a lot more than 2
> occurrences of the phrase:
>
> (turkey shoot){2}
>
> It does't match anything.


It does. It matches, among other things, "turkey shootturkey shoot".

> How do I structure this properly?


/^.*?turkey shoot.*?(turkey shoot)/

Ben

--
Musica Dei donum optimi, trahit homines, trahit deos. |
Musica truces molit animos, tristesque mentes erigit. | (E-Mail Removed)
Musica vel ipsas arbores et horridas movet feras. |
 
Reply With Quote
 
 
 
 
gnari
Guest
Posts: n/a
 
      03-01-2004
"Ray" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Hello,
>
> I'm trying the following to get the 2nd, and only the 2nd occurrence
> of the phrase "turkey shoot", in a string with a lot more than 2
> occurrences of the phrase:
>
> (turkey shoot){2}
>
> It does't match anything.
>
> How do I structure this properly?


it depends really on what you want to do with it
are you talking about /turkey shoot.*(turkey shoot)/ ?

or maybe you want to read perldoc perlre
in particular the bit about zero-width positive look-behind assertions.
something like / (?<=turkey shoot).*(turkey shoot)/

gnari




 
Reply With Quote
 
gnari
Guest
Posts: n/a
 
      03-01-2004
"gnari" <(E-Mail Removed)> wrote in message
news:c207cm$fpn$(E-Mail Removed)...
> "Ray" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > Hello,
> >
> > I'm trying the following to get the 2nd, and only the 2nd occurrence
> > of the phrase "turkey shoot", in a string with a lot more than 2
> > occurrences of the phrase:
> >
> > (turkey shoot){2}
> >
> > It does't match anything.
> >
> > How do I structure this properly?

>
> it depends really on what you want to do with it
> are you talking about /turkey shoot.*(turkey shoot)/ ?

/turkey shoot.*?(turkey shoot)/ of course

>
> or maybe you want to read perldoc perlre
> in particular the bit about zero-width positive look-behind assertions.
> something like / (?<=turkey shoot).*(turkey shoot)/


ditto here: / (?<=turkey shoot).*?(turkey shoot)/

gnari




 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      03-01-2004

"gnari" <(E-Mail Removed)> wrote:
> "gnari" <(E-Mail Removed)> wrote in message
> >
> > or maybe you want to read perldoc perlre
> > in particular the bit about zero-width positive look-behind assertions.
> > something like / (?<=turkey shoot).*(turkey shoot)/

>
> ditto here: / (?<=turkey shoot).*?(turkey shoot)/


Except that what you actually mean is

/ (?<= turkey\ shoot .*? ) turkey\ shoot /x

which unfortunately doesn't work, so some other method must be
employed.

Ben

--
The cosmos, at best, is like a rubbish heap scattered at random.
- Heraclitus
(E-Mail Removed)
 
Reply With Quote
 
gnari
Guest
Posts: n/a
 
      03-02-2004
"Ben Morrow" <(E-Mail Removed)> wrote in message
news:c20fqs$frg$(E-Mail Removed)...
>
> "gnari" <(E-Mail Removed)> wrote:
> > "gnari" <(E-Mail Removed)> wrote in message
> > >
> > > or maybe you want to read perldoc perlre
> > > in particular the bit about zero-width positive look-behind

assertions.
> > > something like / (?<=turkey shoot).*(turkey shoot)/

> >
> > ditto here: / (?<=turkey shoot).*?(turkey shoot)/

>
> Except that what you actually mean is
>
> / (?<= turkey\ shoot .*? ) turkey\ shoot /x


Exept that I actually do not mean this

>
> which unfortunately doesn't work, so some other method must be
> employed.

which is why I did not suggest it

the OP wanted to capture the second occurence
$_='turkey shoot1lkajslkjalsturkey shoot2sdsdfsdturkey shoot3khkj';
print "[$1:$2]\n" if /(?<=turkey shoot).*?(turkey shoot)(\d)/;
prints
[turkey shoot:2]

so what is the problem ?


gnari




 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      03-02-2004

"gnari" <(E-Mail Removed)> wrote:
> "Ben Morrow" <(E-Mail Removed)> wrote in message
> news:c20fqs$frg$(E-Mail Removed)...
> > "gnari" <(E-Mail Removed)> wrote:
> > >
> > > ditto here: / (?<=turkey shoot).*?(turkey shoot)/

> >
> > Except that what you actually mean is
> >
> > / (?<= turkey\ shoot .*? ) turkey\ shoot /x

>
> Exept that I actually do not mean this
>
> > which unfortunately doesn't work, so some other method must be
> > employed.

> which is why I did not suggest it
>
> the OP wanted to capture the second occurence


The OP wanted to *match* the second occurence.

> $_='turkey shoot1lkajslkjalsturkey shoot2sdsdfsdturkey shoot3khkj';
> print "[$1:$2]\n" if /(?<=turkey shoot).*?(turkey shoot)(\d)/;
> prints
> [turkey shoot:2]
>
> so what is the problem ?


What's the point in using (?<=) then?

perl -le'$,="|"; print "turkey shoot1 turkey shoot2 turkey shoot3"
=~ /turkey\ shoot.*?(turkey\ shoot)(\d)/'
turkey shoot|2

I was assuming the reason for using (?<=) was so that the pattern could
be used as s/(?<= turkey\ shoot .*? ) turkey\ shoot/foo/x to replace the
second occurence. Using /(?<=turkey\ shoot).*? turkey\ shoot/x will
replace more than desired.

Ben

--
Heracles: Vulture! Here's a titbit for you / A few dried molecules of the gall
From the liver of a friend of yours. / Excuse the arrow but I have no spoon.
(Ted Hughes, [ Heracles shoots Vulture with arrow. Vulture bursts into ]
/Alcestis/) [ flame, and falls out of sight. ] (E-Mail Removed)
 
Reply With Quote
 
ctcgag@hotmail.com
Guest
Posts: n/a
 
      03-02-2004
(E-Mail Removed) (Ray) wrote:
> Hello,
>
> I'm trying the following to get the 2nd, and only the 2nd occurrence
> of the phrase "turkey shoot", in a string with a lot more than 2
> occurrences of the phrase:


What do you mean by "get"? Since all occurences of the string
"turkey shoot" are indistinguishable, why do you care (and how would you
know) which one you get?

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Bart Lateur
Guest
Posts: n/a
 
      03-03-2004
Ray wrote:

>I'm trying the following to get the 2nd, and only the 2nd occurrence
>of the phrase "turkey shoot", in a string with a lot more than 2
>occurrences of the phrase:
>
>(turkey shoot){2}
>
>It does't match anything.


I hope matching first till second appearance is OK.

/(turkey shoot).*?\1/

--
Bart.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
replacing repetition of characters in string David Rio Ruby 4 06-21-2009 03:18 AM
Re: Boring repetition anthonyberet Computer Support 1 08-06-2003 12:57 AM
Re: Boring repetition trout Computer Support 0 08-05-2003 10:09 PM
Re: Boring repetition philo Computer Support 0 08-05-2003 09:56 PM



Advertisments