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

R

Ray

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.
 
B

Ben Morrow

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
 
G

gnari

Ray said:
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
 
G

gnari

gnari said:
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
 
B

Ben Morrow

gnari said:
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
 
G

gnari

Ben Morrow said:
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
 
B

Ben Morrow

gnari said:
Exept that I actually do not mean this

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
 
C

ctcgag

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
 
B

Bart Lateur

Ray said:
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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top