Please help with regexp - finding all matches?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Boris Pelakh

    Please help with regexp - finding all matches?

    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
  • Jürgen Exner

    #2
    Re: Please help with regexp - finding all matches?

    Boris Pelakh wrote:[color=blue]
    > 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",[/color]

    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


    Comment

    • Jim Gibson

      #3
      Re: Please help with regexp - finding all matches?

      In article <44ec9ada.04040 80535.18cc7c32@ posting.google. com>, Boris
      Pelakh <pelakh@yahoo.c om> wrote:
      [color=blue]
      > 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?[/color]
      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.
      [color=blue]
      > Ideally I want to just assign the result of the match to a hash.[/color]

      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.

      Comment

      • Purl Gurl

        #4
        Re: Please help with regexp - finding all matches?

        Boris Pelakh wrote:

        (snipped)
        [color=blue]
        > I'm trying to extract name:value pairs[/color]
        [color=blue]
        > $f = "a:b,c:d,e: f";
        > @a = ($f =~ m/(?:(\w+):(\w+)) (?:,(?:(\w+):(\ w+)))+/);[/color]
        [color=blue]
        > I should be seeing "a b c d e f", but I am only getting "a b e f",[/color]


        #!perl

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

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

        print "@a";


        Purl Gurl

        Comment

        Working...