Pattern Match With $ (2)

Discussion in 'Perl Misc' started by Cheok Yan Cheng, Oct 6, 2003.

  1. soly for the previous msg. here, i narrow down my problem to
    --------------------------------------------------------------------------------
    #!/usr/intel/bin/perl

    $ebb = '$ebb';
    if($ebb =~ /${ebb}/)
    {
    print $ebb." match\n";
    }
    else
    {
    print $ebb." not match\n"
    }

    $banana = 'banana';
    if($banana =~ /${banana}/)
    {
    print $banana." match\n";
    }
    else
    {
    print $banana." not match\n";
    }
    -------------------------------------------------------------------------------
    i expect both are match. but i got the result:
    -------------------------------------------------------------------------------
    $ebb not match
    banana match
    -------------------------------------------------------------------------------

    how can i solve this problem?

    thank you.

    regards
    yccheok
    Cheok Yan Cheng, Oct 6, 2003
    #1
    1. Advertising

  2. Cheok Yan Cheng

    Lack Mr G M Guest

    In article <>, (Cheok Yan Cheng) writes:
    |>
    |> i expect both are match. but i got the result:
    |>...
    |> how can i solve this problem?

    > $ebb = '$ebb';
    > if($ebb =~ /${ebb}/)


    This replaces ${ebb} with it's value and ends up doing:

    if ($ebb =~ /$ebb/)

    Now, since variable interpolation has already been done the '$' here
    means end-of-line. So the match fails.

    You can "see" what is happenign if you add:

    use re 'debug';

    to the start of the script.

    The cure is to use:

    if ($ebb =~ /\Q${ebb}/)

    if you want all of characaters in ${ebb} to be treated literally. Of
    course, if the contents may be a regular expression then you have to
    take care when setting it up instead.


    --
    --------- Gordon Lack --------------- ------------
    This message *may* reflect my personal opinion. It is *not* intended
    to reflect those of my employer, or anyone else.
    Lack Mr G M, Oct 6, 2003
    #2
    1. Advertising

  3. (Lack Mr G M) wrote in message news:<2003Oct6.112800@ukwit01>...
    > In article <>, (Cheok Yan Cheng) writes:
    > |>
    > |> i expect both are match. but i got the result:
    > |>...
    > |> how can i solve this problem?
    >
    > > $ebb = '$ebb';
    > > if($ebb =~ /${ebb}/)

    >
    > This replaces ${ebb} with it's value and ends up doing:
    >
    > if ($ebb =~ /$ebb/)
    >
    > Now, since variable interpolation has already been done the '$' here
    > means end-of-line. So the match fails.
    >
    > You can "see" what is happenign if you add:
    >
    > use re 'debug';
    >
    > to the start of the script.
    >
    > The cure is to use:
    >
    > if ($ebb =~ /\Q${ebb}/)
    >
    > if you want all of characaters in ${ebb} to be treated literally. Of
    > course, if the contents may be a regular expression then you have to
    > take care when setting it up instead.


    what if i want to match a string '$ebb.xyz'
    i use

    $ebb = '$ebb.xyz';
    if($ebb =~ /^\Q${ebb}(\.)xyz/)
    {
    print "done :)\n";
    }
    else
    {
    print "not work :(\n";
    }

    but the result i get is
    not work :(

    may i noe how can i solve this problem?

    thank you.

    regards
    yccheok
    Cheok Yan Cheng, Oct 7, 2003
    #3
  4. Cheok Yan Cheng

    Bob Walton Guest

    Cheok Yan Cheng wrote:

    > (Lack Mr G M) wrote in message news:<2003Oct6.112800@ukwit01>...
    >
    >>In article <>, (Cheok Yan Cheng) writes:

    ....
    > what if i want to match a string '$ebb.xyz'
    > i use
    >
    > $ebb = '$ebb.xyz';
    > if($ebb =~ /^\Q${ebb}(\.)xyz/)
    > {
    > print "done :)\n";
    > }
    > else
    > {
    > print "not work :(\n";
    > }
    >
    > but the result i get is
    > not work :(



    It works, just not like you seem to think it should. The \Q
    construction quotes all the following metacharacters until a \E (which
    isn't present, so that means to the end of the regexp). The string you
    are trying to match is:

    '$ebb.xyz'

    The pattern you are trying to match it to is (after interpolation and
    the application of \Q quoting):

    /^\$ebb\.xyz\(\\\.\)xyz/

    There is no match because the string being matched doesn't contain a (
    character as the character with offset 8 from the start of the string as
    the regexp requires.


    >
    > may i noe how can i solve this problem?



    What problem? Your lack of understanding? Read and study the docs
    thoroughly, study a book like "Mastering Regular Expressions"
    thoroughly, and develop an understanding of regular expressions. Then
    you'll be able to solve it in your sleep. Until then, don't just guess
    at it ... read the docs. And try:

    use re 'debug';

    as someone else suggested. Take learning regexp's one step at a time --
    you are in semi-difficult territory, and you don't yet understand the
    basics.


    ....


    > yccheok
    >


    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
    Bob Walton, Oct 7, 2003
    #4
    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:
    637
  2. Victor
    Replies:
    2
    Views:
    644
    Victor
    May 17, 2004
  3. ekzept
    Replies:
    0
    Views:
    370
    ekzept
    Aug 10, 2007
  4. John Gordon
    Replies:
    13
    Views:
    482
    Ian Kelly
    Dec 20, 2011
  5. Volkan Civelek

    Match doesn't match

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

Share This Page