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

Discussion in 'Perl Misc' started by Ray, Mar 1, 2004.

  1. Ray

    Ray Guest

    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.
    Ray, Mar 1, 2004
    #1
    1. Advertising

  2. Ray

    Ben Morrow Guest

    (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. |
    Musica vel ipsas arbores et horridas movet feras. |
    Ben Morrow, Mar 1, 2004
    #2
    1. Advertising

  3. Ray

    gnari Guest

    "Ray" <> wrote in message
    news:...
    > 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
    gnari, Mar 1, 2004
    #3
  4. Ray

    gnari Guest

    "gnari" <> wrote in message
    news:c207cm$fpn$...
    > "Ray" <> wrote in message
    > news:...
    > > 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
    gnari, Mar 1, 2004
    #4
  5. Ray

    Ben Morrow Guest

    "gnari" <> wrote:
    > "gnari" <> 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
    Ben Morrow, Mar 1, 2004
    #5
  6. Ray

    gnari Guest

    "Ben Morrow" <> wrote in message
    news:c20fqs$frg$...
    >
    > "gnari" <> wrote:
    > > "gnari" <> 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
    gnari, Mar 2, 2004
    #6
  7. Ray

    Ben Morrow Guest

    "gnari" <> wrote:
    > "Ben Morrow" <> wrote in message
    > news:c20fqs$frg$...
    > > "gnari" <> 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. ]
    Ben Morrow, Mar 2, 2004
    #7
  8. Ray

    Guest

    (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
    , Mar 2, 2004
    #8
  9. Ray

    Bart Lateur Guest

    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.
    Bart Lateur, Mar 3, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    361
  2. artem

    permutation with repetition

    artem, Feb 23, 2007, in forum: Java
    Replies:
    6
    Views:
    2,124
  3. bullockbefriending bard

    Generator for k-permutations without repetition

    bullockbefriending bard, Jul 4, 2007, in forum: Python
    Replies:
    4
    Views:
    1,047
    =?ISO-8859-1?Q?Nis_J=F8rgensen?=
    Jul 4, 2007
  4. David Rio
    Replies:
    4
    Views:
    109
    Morris Keesan
    Jun 21, 2009
  5. Michele Dondi
    Replies:
    0
    Views:
    174
    Michele Dondi
    Nov 15, 2007
Loading...

Share This Page