regrex pattern matching

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • johnhelen
    New Member
    • Sep 2007
    • 8

    regrex pattern matching

    Hello

    I want to parse a String with empty spaces to separate fields into a hash:

    [code=perl]
    use strict;
    use warnings;
    my $data = 'ACCESS:NO PROXYNAME:local host URL:http://localhost/ MDN:none FREE:YES PAID:NO';

    my %values = split(/[: ]/, $data);
    foreach my $k (keys %values)
    {
    print "$k: $values{$k}\n";
    }
    [/code]

    However, the way above did not work as there is two ":" in the pattern "URL:http://localhost/".

    It only works when I change it into "URL:http//localhost/" (remove one ":" character). Could you please help

    Many thanks
    john
    Last edited by numberwhun; Sep 23 '07, 02:02 AM. Reason: add code tags
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    I misread the question so I removed my comments

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      one way:

      [CODE=perl]
      use strict;
      use warnings;
      my $data = 'ACCESS:NO PROXYNAME:local host URL:http://localhost/ MDN:none FREE:YES PAID:NO';
      my %values = ();
      my @data = split(/\s+/,$data); # first split fields on the spaces
      foreach my $var (@data) {
      my($k,$v) = split(/:/,$var,2); # split each field into two string only
      $values{$k} = $v;
      }
      foreach my $k (keys %values){
      print "$k = $values{$k}\n";
      }[/CODE]

      You could also use a regular expression to split the string into key/value pairs and build the hash but the above should be fast and efficient as long as there are no spaces in the keys or values themselves.

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Originally posted by KevinADC
        one way:

        [CODE=perl]
        use strict;
        use warnings;
        my $data = 'ACCESS:NO PROXYNAME:local host URL:http://localhost/ MDN:none FREE:YES PAID:NO';
        my %values = ();
        my @data = split(/\s+/,$data); # first split fields on the spaces
        foreach my $var (@data) {
        my($k,$v) = split(/:/,$var,2); # split each field into two string only
        $values{$k} = $v;
        }
        foreach my $k (keys %values){
        print "$k = $values{$k}\n";
        }[/CODE]

        You could also use a regular expression to split the string into key/value pairs and build the hash but the above should be fast and efficient as long as there are no spaces in the keys or values themselves.
        Its funny, I was wondering how to exclude the second :, but it looks like split will do that since there are only two variables to populate. Very interesting. Thanks for the lesson Kevin!

        Jeff

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          It can be done in "one shot" but I think this is probably less efficient but without benchmarkling I am not sure:

          [CODE=perl]
          my $data = 'ACCESS:NO PROXYNAME:local host URL:http://localhost/ MDN:none FREE:YES PAID:NO';
          my %values = map{ $_->[0],$_->[1] } map {[split(/:/,$_,2)]} split(/\s+/,$data);
          foreach my $k (keys %values){
          print "$k = $values{$k}\n";
          }[/CODE]

          it certainly is less readable/understandable for the casual perl coder.

          Comment

          • johnhelen
            New Member
            • Sep 2007
            • 8

            #6
            thank you very much, Kevin ...

            Comment

            Working...