Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Pattern Match With $

Reply
Thread Tools

Pattern Match With $

 
 
Cheok Yan Cheng
Guest
Posts: n/a
 
      10-06-2003
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
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      10-06-2003
Cheok Yan Cheng <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      10-06-2003
Cheok Yan Cheng <(E-Mail Removed)> 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

 
Reply With Quote
 
ko
Guest
Posts: n/a
 
      10-06-2003
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

 
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