Help - Search - Member List - Calendar
Full Version: regex problem
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
John Moon
The following is not returning what I had expected...

SUN1-BATCH>perl -e '$a=q{/var/run}; $home=q{/var/123};print "Yes - $a like
$homen" if $a =~ /^$home/;'
SUN1-BATCH>perl -e '$a=q{/var/run}; $home=q{/var/ra};print "Yes - $a like
$homen" if $a =~ /^$home/;'
SUN1-BATCH>perl -e '$a=q{/var/run}; $home=q{/var/ru};print "Yes - $a like
$homen" if $a =~ /^$home/;'
Yes - /var/run like /var/ru


I would have "assumed" that /var/run would NOT be "like" /var/ru just as
/var/run is not "like" /var/ra...

John W Moon

Chris Devers
On Fri, 1 Jul 2005, Moon, John wrote:

QUOTE
The following is not returning what I had expected...

$a    = q{/var/run};
$home = q{/var/ru};
print "Yes - $a like $homen" if $a =~ /^$home/;

I would have "assumed"  that /var/run would NOT be "like" /var/ru just
as /var/run is not "like" /var/ra...

It depends what you mean by "like".

In this case, the string in $home also appears as part of $a, so in that
sense there are "alike", and the code is doing the right thing.

If you want to verify that $a and $home are identical, it would be
easier to just check if one `eq` the other, as

print "Yes - $a like $homen" if $a eq $home;

That test will work if $home is '/var/run', but will fail on '/var/ru'.


--
Chris Devers

Ing. Branislav Gerzo
Moon, John [MJ], on Friday, July 1, 2005 at 11:30 (-0400 ) contributed
this to our collective wisdom:

MJ> I would have "assumed" that /var/run would NOT be "like" /var/ru just as
MJ> /var/run is not "like" /var/ra...

is "/var/ru" at the beginning of "/var/run" ? yes.

--

...m8s, cu l8r, Brano.

[If they get too annoying then we'll just have to get violent...]

Jay Savage
On 7/1/05, Moon, John <[Email Removed]> wrote:
QUOTE
The following is not returning what I had expected...

SUN1-BATCH>perl -e '$a=q{/var/run}; $home=q{/var/123};print "Yes - $a like
$homen" if $a =~ /^$home/;'
SUN1-BATCH>perl -e '$a=q{/var/run}; $home=q{/var/ra};print "Yes - $a like
$homen" if $a =~ /^$home/;'
SUN1-BATCH>perl -e '$a=q{/var/run}; $home=q{/var/ru};print "Yes - $a like
$homen" if $a =~ /^$home/;'
Yes - /var/run like /var/ru


I would have "assumed"  that /var/run would NOT be "like" /var/ru just as
/var/run is not "like" /var/ra...

John W Moon


John

A regex match checks to see if the specified pattern appears in the
specified string. And the answer to the question "is /var/ru in
/var/run?" is "yes." Or to put it another way:

$a =~ /$home/

is functionally (although not proceedurally) equivalent to:

$a =~ /^.*$home.*$/

If you want to do a simple test for equality, use 'eq'. If you're
going to test for a pattern and want to match on the entire string,
anchor the patern at the beginning and end of the string:

$a =~ /^$home$/

but if $home is a simple string without regex metacharaters 'eq' it
going to be a lot faster than m//.

HTH,

-- jay
--------------------
daggerquill [at] gmail [dot] com
http://www.tuaw.com
http://www.dpguru.com
http://www.engatiki.org


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2005 Invision Power Services, Inc.