pattern match

Discussion in 'Perl Misc' started by Venkatesh can....can..., Mar 20, 2008.

  1. $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...
    Venkatesh can....can..., Mar 20, 2008
    #1
    1. Advertising

  2. 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 Hjalmarsson, Mar 20, 2008
    #2
    1. Advertising

  3. Venkatesh can....can...

    Guest

    On Mar 20, 11:02 am, Gunnar Hjalmarsson <> 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?
    , Mar 20, 2008
    #3
  4. wrote:
    > On Mar 20, 11:02 am, Gunnar Hjalmarsson <> 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
    Gunnar Hjalmarsson, Mar 20, 2008
    #4
  5. <> wrote:
    > On Mar 20, 11:02 am, Gunnar Hjalmarsson <> 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/"
    Tad J McClellan, Mar 20, 2008
    #5
    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. hiwa
    Replies:
    0
    Views:
    636
  2. Victor
    Replies:
    2
    Views:
    640
    Victor
    May 17, 2004
  3. ekzept
    Replies:
    0
    Views:
    365
    ekzept
    Aug 10, 2007
  4. John Gordon
    Replies:
    13
    Views:
    477
    Ian Kelly
    Dec 20, 2011
  5. Volkan Civelek

    Match doesn't match

    Volkan Civelek, Jul 19, 2006, in forum: Ruby
    Replies:
    4
    Views:
    163
Loading...

Share This Page