Using end of line in character class

Discussion in 'Perl Misc' started by Nilesh, Jun 25, 2009.

  1. Nilesh

    Nilesh Guest

    Hi,

    I am trying extract specific value out of URL query. Here is the
    example.

    my $text = "http://localhost/news.xmlapi/
    StoryResponse.aspxServ=RT&Pkey=1245445784nSP376208-20090619210944-2-
    FRT";
    #my $text = "http://localhost/news.xmlapi/StoryResponse.aspx?
    Pkey=124544 84nSP376208-20090619210944-2-FRT&Serv=RT";

    if($text =~ m/&*Serv=(.*)[&$]/)
    {
    printf "$1\n";
    } else {
    printf "not matched";
    }

    it gives follwing error.
    Unmatched [ in regex; marked by <-- HERE in m/&*Serv=(.*)[ <-- HERE
    &5.008008/ at ./x.pl line 6

    If I remove $ from character class then it works fine for first text.
    But it does not work for commented definition of text which is also
    possible case.

    I don't know what is wrong with $(end of line) in character classs.

    Thanks,
    Nilesh
     
    Nilesh, Jun 25, 2009
    #1
    1. Advertising

  2. Nilesh schrieb:
    > if($text =~ m/&*Serv=(.*)[&$]/)
    >
    > it gives follwing error.
    > Unmatched [ in regex; marked by <-- HERE in m/&*Serv=(.*)[ <-- HERE
    > &5.008008/ at ./x.pl line 6


    $] is a variable containing the Perl version. See perldoc perlvar.

    But then, as you are using a character class, you are looking for the
    letter "$", not for the end of a line. So don't use a character class.
    You need to look for "&" OR end of line: (&|$). Then, .* is greedy,
    that's not what you want, so add a ?. Additionally, &* in the beginning
    of your regexp is useless.

    You propably want something like
    -> if ($text =~ /Serv=(.*?)(?:&|$)/) { ...

    - Wolf
     
    Wolf Behrenhoff, Jun 25, 2009
    #2
    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. Wai Yip Tung
    Replies:
    5
    Views:
    1,116
    Andrew Dalke
    Sep 4, 2004
  2. Andreas Leitgeb
    Replies:
    0
    Views:
    453
    Andreas Leitgeb
    May 15, 2009
  3. Mark Space
    Replies:
    0
    Views:
    488
    Mark Space
    May 15, 2009
  4. Lew
    Replies:
    0
    Views:
    951
  5. Joshua Cranmer
    Replies:
    0
    Views:
    464
    Joshua Cranmer
    May 15, 2009
Loading...

Share This Page