Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   pattern match (http://www.velocityreviews.com/forums/t906831-pattern-match.html)

Venkatesh can....can... 03-20-2008 05:29 AM

pattern match
 
$var="{' venkat'}->{'no'}->{'yes'}";
i want to get the "yes" token;
if i use
$var=~/\{'( .* )\}$/
i get venkat'}->{'no'}->{'yes
how to get the "yes" token...

Gunnar Hjalmarsson 03-20-2008 06:02 AM

Re: pattern match
 
Venkatesh can....can... wrote:
> $var="{' venkat'}->{'no'}->{'yes'}";
> i want to get the "yes" token;
> if i use
> $var=~/\{'( .* )\}$/
> i get venkat'}->{'no'}->{'yes


No you don't. You get nothing, because that regex does not match.
However, with the /x modifier it matches and assigns the string you
mention to $1.

> how to get the "yes" token...


One way:

$var =~ /.+{'(.+)'}$/;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

venkatesh.naughty@gmail.com 03-20-2008 06:18 AM

Re: pattern match
 
On Mar 20, 11:02*am, Gunnar Hjalmarsson <nore...@gunnar.cc> wrote:
> Venkatesh can....can... wrote:
> > $var="{' venkat'}->{'no'}->{'yes'}";
> > i want to get the "yes" token;
> > if i use
> > $var=~/\{'( .* )\}$/
> > i get venkat'}->{'no'}->{'yes

>
> No you don't. You get nothing, because that regex does not match.
> However, with the /x modifier it matches and assigns the string you
> mention to $1.
>
> > how to get the "yes" token...

>
> One way:
>
> * * *$var =~ /.+{'(.+)'}$/;
>
> --
> Gunnar Hjalmarsson
> Email:http://www.gunnar.cc/cgi-bin/contact.pl


@gunnar

thanks it works but how?
the first .+ is greedy know? it 'll match up to the end right?

Gunnar Hjalmarsson 03-20-2008 10:13 AM

Re: pattern match
 
venkatesh.naughty@gmail.com wrote:
> On Mar 20, 11:02 am, Gunnar Hjalmarsson <nore...@gunnar.cc> wrote:
>> Venkatesh can....can... wrote:
>>>
>>> $var="{' venkat'}->{'no'}->{'yes'}";
>>> i want to get the "yes" token;
>>> if i use
>>> $var=~/\{'( .* )\}$/
>>> i get venkat'}->{'no'}->{'yes

>>
>> No you don't. You get nothing, because that regex does not match.
>> However, with the /x modifier it matches and assigns the string you
>> mention to $1.
>>
>>> how to get the "yes" token...

>>
>> One way:
>>
>> $var =~ /.+{'(.+)'}$/;

>
> thanks it works but how?
> the first .+ is greedy know?


Yes.

> it 'll match up to the end right?


No, it matches one or more characters as long as it can without
preventing the whole regex from matching; in this case up to and
including the second arrow.

Remember that greediness never affects whether a regex matches or not.
It just may affect _how_ the regex matches.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Tad J McClellan 03-20-2008 10:43 AM

Re: pattern match
 
venkatesh.naughty@gmail.com <venkatesh.naughty@gmail.com> wrote:
> On Mar 20, 11:02*am, Gunnar Hjalmarsson <nore...@gunnar.cc> wrote:
>> Venkatesh can....can... wrote:
>> > $var="{' venkat'}->{'no'}->{'yes'}";
>> > i want to get the "yes" token;


>>
>> * * *$var =~ /.+{'(.+)'}$/;
>>
>> --
>> Gunnar Hjalmarsson
>> Email:http://www.gunnar.cc/cgi-bin/contact.pl

>
> @gunnar



[ it is bad manners to quote .sigs, please do not do that. ]


> the first .+ is greedy know? it 'll match up to the end right?



Right.

But then the regex engine will notice that the match will fail,
so it "backtracks" and attempts the match again.

How Regexes Work:

http://perl.plover.com/Regex/article.html


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


All times are GMT. The time now is 07:34 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.