Parsing in regular expression

Discussion in 'Perl Misc' started by sam, Jan 19, 2005.

  1. sam

    sam Guest

    Hi,

    The following pattern does not return all the values, the pattern only
    able to parse the first two fields:

    my $test = "01-, Revlon, n/a, Revlon";
    $test =~
    /^([0-9]*-), #prefix 1
    (.*), #vendor 2
    (.*), #Category 3
    (.*) #basename 4
    /x;

    if ($1 eq "" or $2 eq "" or $3 eq "") {
    print "Failed to parse $test\n";
    } else {
    # get the value
    }

    What should be the correct implementation of this pattern?
    Thanks
    Sam
     
    sam, Jan 19, 2005
    #1
    1. Advertising

  2. sam <> writes:
    >
    > The following pattern does not return all the values, the pattern only
    > able to parse the first two fields:
    >
    > my $test = "01-, Revlon, n/a, Revlon";
    > $test =~
    > /^([0-9]*-), #prefix 1
    > (.*), #vendor 2
    > (.*), #Category 3
    > (.*) #basename 4
    > /x;
    >
    > if ($1 eq "" or $2 eq "" or $3 eq "") {
    > print "Failed to parse $test\n";
    > } else {
    > # get the value
    > }


    It works for me - I get the respective fields in all $1-$4.
    " n/a" in $3, in particular. What do you get in $3?

    It's better to test the match itself for success:
    if ($test =~ ...)
    but that doesn't explain why the match doesn't work for you.
     
    Arndt Jonasson, Jan 19, 2005
    #2
    1. Advertising

  3. sam <> wrote:


    > The following pattern does not return all the values, the pattern only
    > able to parse the first two fields:
    >
    > my $test = "01-, Revlon, n/a, Revlon";



    > What should be the correct implementation of this pattern?


    my @fields = split /,\s*/, $test;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jan 19, 2005
    #3
    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. VSK
    Replies:
    2
    Views:
    2,387
  2. Bill Chiu
    Replies:
    4
    Views:
    447
    Uwe Schnitker
    Sep 12, 2003
  3. ArdGre
    Replies:
    9
    Views:
    500
    Mike Schilling
    Jan 9, 2007
  4. Replies:
    3
    Views:
    360
    Paddy
    Oct 29, 2006
  5. abcd
    Replies:
    2
    Views:
    317
Loading...

Share This Page