orthanc has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

Another annoying which needs some help !

I've got a string which is

blah blah (name 1, name 2, name 3 and name 4), blah blah (name 1, name 2, name 3 and name 4)

I need a regex which will catch all the commas within () but not the comma outside also this string could be repeated N times.

Hope u ppl can shed some light on the problem,

Thx Monks, Orthanc

Replies are listed 'Best First'.
Re: Regex substitution
by merlyn (Sage) on Sep 09, 2000 at 19:56 UTC
    Once you "catch" them, what do you want to do with them?

    Taking one wild guess, which could be totally off, because I can think of about three or four things you might mean, let's say you want to grab all the items that are comma-delimited inside the parens:

    $string = "blah blah (name 1, name 2, name 3 and name 4), blah blah (n +ame 1, name 2, name 3 and name 4)"; @items = map { split /,\*/ } $string =~ /\((.*?)\)/g;

    -- Randal L. Schwartz, Perl hacker

      @items = map { split /,\*/ } $string =~ /\((.*?)\)/g;

      Tsk. And after Ovid and you went through the rounds in Death to Dot Star!

      (although I don't know which is actually faster)

Re: Regex substitution
by tye (Sage) on Sep 09, 2000 at 20:16 UTC
    s# ([(][^)]*[)]) # my $x=$1; $x =~ s/,/;/g; $x #xeg

    Update: I added the "; $x" that I originally left out. It makes me long for an extra modifier for s/// that copies the string before modifying it and then returns the modified string. It would be perfect for use with map.

            - tye (but my friends call me "Tye")
Re: Regex substitution
by orthanc (Monk) on Sep 09, 2000 at 20:07 UTC

    Sorry for the vagueness

    I want to substitute them for something other than a comma

    Orthanc

Re: Regex substitution
by orthanc (Monk) on Sep 09, 2000 at 20:53 UTC

    Sorry for the hassle guys
    I'm a bit rushed today

    Start String
    team 1 (name, name, name and name), team 2 (name, name, name and blah)

    End String
    team 1 (name; name; name and name), team 2 (name; name; name and blah)

    Hope this helps :)

    Orthanc

Re: Regex substitution
by orthanc (Monk) on Sep 09, 2000 at 21:03 UTC

    Big Thanks to all that helped, much appreciated. I was pulling my hair out :)

    Orthanc

Re: Regex substitution
by orthanc (Monk) on Sep 09, 2000 at 20:24 UTC

    Tye

    Your nice regex doesn't seem to work for me

    Orthanc

      We've been trying to help you. Give us an example of a "start string", and an "end string".

      Your specifications can only leave us wondering.

      -- Randal L. Schwartz, Perl hacker