Please help with regexp - finding all matches?

Discussion in 'Perl' started by Boris Pelakh, Apr 8, 2004.

  1. Boris Pelakh

    Boris Pelakh Guest

    I'm trying to extract name:value pairs from a string similar to this one

    {a:b,c:d,e:f}

    The individual pieces are more complicated, but its similar in principal

    Here is a test script I use

    $f = "a:b,c:d,e:f";
    @a = ($f =~ m/(?:(\w+):(\w+))(?:,(?:(\w+):(\w+)))+/);
    print "@a\n";

    I should be seeing "a b c d e f", but I am only getting "a b e f",
    i.e. the first and last group matched. Why am I not seeing the rest?
    Ideally I want to just assign the result of the match to a hash.

    -- Boris
    Boris Pelakh, Apr 8, 2004
    #1
    1. Advertising

  2. Boris Pelakh wrote:
    > Here is a test script I use
    >
    > $f = "a:b,c:d,e:f";
    > @a = ($f =~ m/(?:(\w+):(\w+))(?:,(?:(\w+):(\w+)))+/);
    > print "@a\n";
    >
    > I should be seeing "a b c d e f", but I am only getting "a b e f",


    What about a simple

    use strict;
    use warnings;
    my $f = "a:b,c:d,e:f";
    my @a;
    for (split /,/, $f) {
    push @a ,(split /:/);
    }
    print "@a\n";

    In my opinion much easier to write and to read than a complicated RE.

    jue
    Jürgen Exner, Apr 8, 2004
    #2
    1. Advertising

  3. Boris Pelakh

    Jim Gibson Guest

    In article <>, Boris
    Pelakh <> wrote:

    > I'm trying to extract name:value pairs from a string similar to this one
    >
    > {a:b,c:d,e:f}
    >
    > The individual pieces are more complicated, but its similar in principal
    >
    > Here is a test script I use
    >
    > $f = "a:b,c:d,e:f";
    > @a = ($f =~ m/(?:(\w+):(\w+))(?:,(?:(\w+):(\w+)))+/);
    > print "@a\n";
    >
    > I should be seeing "a b c d e f", but I am only getting "a b e f",
    > i.e. the first and last group matched. Why am I not seeing the rest?

    You only see "a b e f" in your output because you only have 4 capturing
    parentheses in your pattern, and you only apply your pattern once. The
    entire string is matched due to the '+' after the second subgrouping,
    but the capturing bits in this group are applied twice: first to
    capture the 'c' and the 'd', and then to match and capture the 'e' and
    the 'f', overwriting the 'c' and 'd'.

    If you want all of the matching bits, use a simpler pattern but use it
    repeatedly with the 'g' modifier:

    my @a = ( $f =~ m/(?:(\w+):(\w+))/g );

    but, as Jürgen pointed out, using split twice is simpler.

    > Ideally I want to just assign the result of the match to a hash.


    Well, then, assign the results of the match to a hash:

    my %h = ( $f =~ m/(?:(\w+):(\w+))/g );

    FYI: this newsgroup is defunct. Try comp.lang.perl.misc in the future.
    Jim Gibson, Apr 8, 2004
    #3
  4. Boris Pelakh

    Purl Gurl Guest

    Boris Pelakh wrote:

    (snipped)

    > I'm trying to extract name:value pairs


    > $f = "a:b,c:d,e:f";
    > @a = ($f =~ m/(?:(\w+):(\w+))(?:,(?:(\w+):(\w+)))+/);


    > I should be seeing "a b c d e f", but I am only getting "a b e f",



    #!perl

    $f = "a:b,c:d,e:f";

    @a = split (/:|,/, $f);

    print "@a";


    Purl Gurl
    Purl Gurl, Apr 8, 2004
    #4
    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:
    3
    Views:
    1,562
  2. gry
    Replies:
    4
    Views:
    224
  3. Kevin Howe

    multiple regexp matches

    Kevin Howe, Aug 17, 2004, in forum: Ruby
    Replies:
    27
    Views:
    275
    Martin DeMello
    Aug 24, 2004
  4. Joao Silva
    Replies:
    16
    Views:
    359
    7stud --
    Aug 21, 2009
  5. Roy Smith

    Finding all regex matches by index?

    Roy Smith, May 30, 2012, in forum: Python
    Replies:
    1
    Views:
    145
    Roy Smith
    May 30, 2012
Loading...

Share This Page