Using end of line in character class

N

Nilesh

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
 
W

Wolf Behrenhoff

Nilesh said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top