Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Using end of line in character class

Reply
Thread Tools

Using end of line in character class

 
 
Nilesh
Guest
Posts: n/a
 
      06-25-2009
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

 
Reply With Quote
 
 
 
 
Wolf Behrenhoff
Guest
Posts: n/a
 
      06-25-2009
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: illegal line end in character literal in escaped unicode CARRIAGERETURN / NEW LINE Joshua Cranmer Java 0 05-15-2009 02:50 PM
Re: illegal line end in character literal in escaped unicode CARRIAGERETURN / NEW LINE Lew Java 0 05-15-2009 02:38 PM
Re: illegal line end in character literal in escaped unicode CARRIAGERETURN / NEW LINE Mark Space Java 0 05-15-2009 02:35 PM
Re: illegal line end in character literal in escaped unicode CARRIAGE RETURN / NEW LINE Andreas Leitgeb Java 0 05-15-2009 02:02 PM
read lines without the line break character at the end? Wai Yip Tung Python 5 09-04-2004 09:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57