Pattern Match With $

  • Thread starter Cheok Yan Cheng
  • Start date
C

Cheok Yan Cheng

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
 
A

Anno Siegel

Cheok Yan Cheng said:
i have a file named 'list' with content:
--------------------------------------------------------------------------------
$ebb.runrv_out
$ebb.emsh_log
rv/$ebb.rv.audit
$ebb.notexist

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

[...]

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
 
A

Anno Siegel

Cheok Yan Cheng said:
i have a file named 'list' with content:
--------------------------------------------------------------------------------
$ebb.runrv_out
$ebb.emsh_log
rv/$ebb.rv.audit
$ebb.notexist

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

[...]

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
 
K

ko

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top