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. Advertisements

  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. Advertisements

  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. Advertisements

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,651
  2. Bill Chiu
    Replies:
    4
    Views:
    504
    Uwe Schnitker
    Sep 12, 2003
  3. ArdGre
    Replies:
    9
    Views:
    654
    Mike Schilling
    Jan 9, 2007
  4. Replies:
    3
    Views:
    406
    Paddy
    Oct 29, 2006
  5. abcd
    Replies:
    2
    Views:
    361
  6. Sebastian probst Eide

    Parsing text with regular expression

    Sebastian probst Eide, Apr 29, 2007, in forum: Ruby
    Replies:
    2
    Views:
    191
    Sebastian probst Eide
    Apr 29, 2007
  7. Leif Wessman

    parsing XML using a regular expression

    Leif Wessman, Sep 8, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    358
    Tim Green
    Sep 9, 2004
  8. Replies:
    8
    Views:
    252
    William James
    Sep 21, 2005
Loading...