Pattern Match With $

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

  1. i have a file named 'list' with content:
    --------------------------------------------------------------------------------
    $ebb.runrv_out
    $ebb.emsh_log
    rv/$ebb.rv.audit
    $ebb.notexist
    --------------------------------------------------------------------------------
    i try the following code:

    #!/usr/intel/bin/perl

    $ebb = '$ebb';
    open(LIST, "list") || die "$!\n";
    while(<LIST>)
    {
    print;
    print " is going match with $ebb\n";

    if(/^${ebb}(\.)/)
    {
    print "YEAH!\n";
    }
    else
    {
    print "NO!\n";
    }
    }
    close(LIST);


    the output i get is
    --------------------------------------------------------------------------------
    $ebb.runrv_out
    is going match with
    NO!
    $ebb.emsh_log
    is going match with
    NO!
    rv/$ebb.rv.audit
    is going match with
    NO!
    $ebb.notexist
    is going match with
    NO!
    --------------------------------------------------------------------------------
    i expect to get "YEAH" frm pattern

    if(/^${ebb}(\.)/)

    but it failed :( may i noe how can i solve this prob?

    thank you.

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

  2. Cheok Yan Cheng

    Anno Siegel Guest

    Cheok Yan Cheng <> wrote in comp.lang.perl.misc:
    > i have a file named 'list' with content:
    > --------------------------------------------------------------------------------
    > $ebb.runrv_out
    > $ebb.emsh_log
    > rv/$ebb.rv.audit
    > $ebb.notexist
    > --------------------------------------------------------------------------------
    > i try the following code:
    >
    > #!/usr/intel/bin/perl


    No warnings, no strictures. Bad.

    >
    > $ebb = '$ebb';


    There is no reason why $ebb should be a package variable. Declare it
    with "my".

    > open(LIST, "list") || die "$!\n";
    > while(<LIST>)
    > {
    > print;
    > print " is going match with $ebb\n";
    >
    > if(/^${ebb}(\.)/)


    There is no reason for the "{}" around "ebb". If you want a literal "$"
    in a regex, escape it with "\":

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

    Not that this regex doe *not* refer to the variable "$ebb", it contains
    a literal "$", literally followed by "ebb".

    If you *must* keep that part in a variable, use the "\Q" and "\E" escapes
    to protect the "$":

    if( /^\Q$ebb\E(\.)/ )

    > {
    > print "YEAH!\n";
    > }
    > else
    > {
    > print "NO!\n";
    > }
    > }
    > close(LIST);
    >
    >
    > the output i get is
    > --------------------------------------------------------------------------------
    > $ebb.runrv_out
    > is going match with
    > NO!


    [...]

    This is not the output of your program. The second line ought to be
    " is going match with $ebb". Please take care in preparing your post,
    so the reader doesn't have to resolve contradictions.

    Anno
     
    Anno Siegel, Oct 6, 2003
    #2
    1. Advertising

  3. Cheok Yan Cheng

    Anno Siegel Guest

    Cheok Yan Cheng <> wrote in comp.lang.perl.misc:
    > i have a file named 'list' with content:
    > --------------------------------------------------------------------------------
    > $ebb.runrv_out
    > $ebb.emsh_log
    > rv/$ebb.rv.audit
    > $ebb.notexist
    > --------------------------------------------------------------------------------
    > i try the following code:
    >
    > #!/usr/intel/bin/perl


    No warnings, no strictures. Bad.

    >
    > $ebb = '$ebb';


    There is no reason why $ebb should be a package variable. Declare it
    with "my".

    > open(LIST, "list") || die "$!\n";
    > while(<LIST>)
    > {
    > print;
    > print " is going match with $ebb\n";
    >
    > if(/^${ebb}(\.)/)


    There is no reason for the "{}" around "ebb". If you want a literal "$"
    in a regex, escape it with "\":

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

    Note that this regex doe *not* refer to the variable "$ebb", it contains
    a literal "$", literally followed by "ebb".

    If you *must* keep that part in a variable, use the "\Q" and "\E" escapes
    to protect the "$":

    if( /^\Q$ebb\E(\.)/ )

    > {
    > print "YEAH!\n";
    > }
    > else
    > {
    > print "NO!\n";
    > }
    > }
    > close(LIST);
    >
    >
    > the output i get is
    > --------------------------------------------------------------------------------
    > $ebb.runrv_out
    > is going match with
    > NO!


    [...]

    This is not the output of your program. The second line ought to be
    " is going match with $ebb". Please take care in preparing your post,
    so the reader doesn't have to resolve contradictions.

    Anno
     
    Anno Siegel, Oct 6, 2003
    #3
  4. Cheok Yan Cheng

    ko Guest

    Cheok Yan Cheng wrote:

    [snip]

    > i expect to get "YEAH" frm pattern
    >
    > if(/^${ebb}(\.)/)
    >
    > but it failed :( may i noe how can i solve this prob?


    There are some characters that you cannot use literally in a regular
    expression, one of them is '$'. These special characters are called
    metacharacters. You always have to 'escape' the metacharacter either, as
    Anno pointed out with \Q ... \E ($ebb is being interpolated ), or with a
    backslash when you want to match the literal character itself. The list
    of metacharacters (from perlretut) is:

    {}[]()^$.|*+?\

    Reading perlretut and perlre will give you more detail on using regular
    expressions.

    HTH -keith
     
    ko, Oct 6, 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:
    648
  2. Victor
    Replies:
    2
    Views:
    656
    Victor
    May 17, 2004
  3. ekzept
    Replies:
    0
    Views:
    386
    ekzept
    Aug 10, 2007
  4. John Gordon
    Replies:
    13
    Views:
    499
    Ian Kelly
    Dec 20, 2011
  5. Volkan Civelek

    Match doesn't match

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

Share This Page