Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > pattern match

Reply
Thread Tools

pattern match

 
 
Venkatesh can....can...
Guest
Posts: n/a
 
      03-20-2008
$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...
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      03-20-2008
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
 
Reply With Quote
 
 
 
 
venkatesh.naughty@gmail.com
Guest
Posts: n/a
 
      03-20-2008
On Mar 20, 11:02*am, Gunnar Hjalmarsson <(E-Mail Removed)> 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?
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      03-20-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Mar 20, 11:02 am, Gunnar Hjalmarsson <(E-Mail Removed)> 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
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      03-20-2008
(E-Mail Removed) <(E-Mail Removed)> wrote:
> On Mar 20, 11:02*am, Gunnar Hjalmarsson <(E-Mail Removed)> 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/"
 
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.sub(): replace longest match instead of leftmost match? John Gordon Python 13 12-20-2011 02:58 AM
pat-match.lisp or extend-match.lisp in Python? ekzept Python 0 08-10-2007 06:08 PM
Match doesn't match Volkan Civelek Ruby 4 07-19-2006 07:44 AM
$match = true() for empty $match?? Victor XML 2 05-17-2004 10:43 AM
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM



Advertisments