Pattern Match With $ (2)

  • Thread starter Cheok Yan Cheng
  • Start date
C

Cheok Yan Cheng

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
 
L

Lack Mr G M

|>
|> 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.
 
C

Cheok Yan Cheng

|>
|> i expect both are match. but i got the result:
|>...
|> how can i solve this problem?


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
 
B

Bob Walton

Cheok said:
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.


....
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top