Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Basic pattern matching - baffled

Reply
Thread Tools

Basic pattern matching - baffled

 
 
Xainin
Guest
Posts: n/a
 
      10-02-2008
Help! I don't understand why this script:

#!perl -w

$a = 'C:\WINDOWS';
$b = 'C:\WINDOWS';

if ( $a =~ /^$b$/i ) {
print "matched '$a' to '$b'\n";
}
else {
print "UNMATCHED '$a' vs. '$b'\n";
}

$ta = quotemeta "$a";
$tb = quotemeta "$b";
if ( $ta =~ /^$tb$/i ) {
print "(quoted) matched '$ta' to '$tb'\n";
}
else {
print "(quoted) UNMATCHED '$ta' vs. '$tb'\n";
}

__END__

Reports this:

UNMATCHED 'C:\WINDOWS' vs. 'C:\WINDOWS'
(quoted) UNMATCHED 'C\:\\WINDOWS' vs. 'C\:\\WINDOWS'

--
Hot water heaters? Hot water needs heating?
 
Reply With Quote
 
 
 
 
Tim Greer
Guest
Posts: n/a
 
      10-02-2008
Xainin wrote:

> Help! I don't understand why this script:
>
> #!perl -w
>
> $a = 'C:\WINDOWS';
> $b = 'C:\WINDOWS';
>
> if ( $a =~ /^$b$/i ) {
> print "matched '$a' to '$b'\n";
> }
> else {
> print "UNMATCHED '$a' vs. '$b'\n";
> }
>
> $ta = quotemeta "$a";
> $tb = quotemeta "$b";
> if ( $ta =~ /^$tb$/i ) {
> print "(quoted) matched '$ta' to '$tb'\n";
> }
> else {
> print "(quoted) UNMATCHED '$ta' vs. '$tb'\n";
> }
>
> __END__
>
> Reports this:
>
> UNMATCHED 'C:\WINDOWS' vs. 'C:\WINDOWS'
> (quoted) UNMATCHED 'C\:\\WINDOWS' vs. 'C\:\\WINDOWS'
>


The \W is activated in the regular expression as a "non word" character.
The quotemeta will automatically disable (backwack \) characters that
would otherwise be seen as a meta character or such things as ;, \,
etc. are translated as \;, \\, etc.
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      10-02-2008
Xainin <(E-Mail Removed)> wrote:
>Help! I don't understand why this script:
>
>#!perl -w


Most people prefer
use warnings;
and
use strict;

>$a = 'C:\WINDOWS';
>$b = 'C:\WINDOWS';
>
>if ( $a =~ /^$b$/i ) {


You got a variation of 'perldoc -q "dos paths".

You are trying to match 'C:' followed by a non-word character, followed
by 'INDOWS' in the text 'C:\WINDOWS'.

See 'perldoc perlre'

jue
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      10-02-2008
Xainin <(E-Mail Removed)> wrote:
> Help! I don't understand why this script:
>
> #!perl -w
>
> $a = 'C:\WINDOWS';
> $b = 'C:\WINDOWS';
>
> if ( $a =~ /^$b$/i ) {
> print "matched '$a' to '$b'\n";
> }
> else {
> print "UNMATCHED '$a' vs. '$b'\n";
> }


\W is special in a regex.

>
> $ta = quotemeta "$a";


$a is not used as a regex, it is treated as a literal string. Protecting
characters special to regexes in something not used that way is
counterproductive.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
Reply With Quote
 
Xainin
Guest
Posts: n/a
 
      10-03-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

>Xainin <(E-Mail Removed)> wrote:
>> Help! I don't understand why this script:
>>
>> #!perl -w
>>
>> $a = 'C:\WINDOWS';
>> $b = 'C:\WINDOWS';
>>
>> if ( $a =~ /^$b$/i ) {
>> print "matched '$a' to '$b'\n";
>> }
>> else {
>> print "UNMATCHED '$a' vs. '$b'\n";
>> }

>
>\W is special in a regex.
>
>>
>> $ta = quotemeta "$a";

>
>$a is not used as a regex, it is treated as a literal string. Protecting
>characters special to regexes in something not used that way is
>counterproductive.
>
>Xho


Thanks to all - I added strict/warnings and declared with "my", but the
key per your last comment was to change "$ta" to "$a" in my last test and
it works.

--
A waist is a terrible thing to mind.
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      10-04-2008
On Fri, 03 Oct 2008 01:54:11 -0700, Xainin <(E-Mail Removed)> wrote:

>(E-Mail Removed) wrote:
>
>>Xainin <(E-Mail Removed)> wrote:
>>> Help! I don't understand why this script:
>>>
>>> #!perl -w
>>>
>>> $a = 'C:\WINDOWS';
>>> $b = 'C:\WINDOWS';
>>>
>>> if ( $a =~ /^$b$/i ) {
>>> print "matched '$a' to '$b'\n";
>>> }
>>> else {
>>> print "UNMATCHED '$a' vs. '$b'\n";
>>> }

>>
>>\W is special in a regex.
>>
>>>
>>> $ta = quotemeta "$a";

>>
>>$a is not used as a regex, it is treated as a literal string. Protecting
>>characters special to regexes in something not used that way is
>>counterproductive.
>>
>>Xho

>
>Thanks to all - I added strict/warnings and declared with "my", but the
>key per your last comment was to change "$ta" to "$a" in my last test and
>it works.



I'm not sure if you are getting the point.

The regular expression is on the right, what your testing is on the left:

$a = 'C:\WINDOWS';

if ($a =~ /do/i) { # i modifier means case insensitive matching
# regexp ^^
print "matched $a to 'do'\n";
}

if ($a =~ /wi/i) {
print "matched $a to 'wi'\n";
}

if ($a !~ /c:\windows/i) {
# escape seq ^^
print "did NOT match $a to 'c:\windows'\n";

# in this case the regular expression had a \w in it
# which is shorthand for all the letters, and all the
# numbers that can be matched in that single character position.
# the regexp now looks for:
# c, :, then
# any char or number (because of \w), then
# i, n, d, o, w, s
# however, in that character position in $a, the literal object
# of comparison, is '\' and it fails
}

# to fix that, the regular expression needs to escape '\' the escape character.
# this results in '\\'w instead of '\w'. there is no '\\' substitution (shorthand)
# in the regex parser, so '\\' is treated as a single '\' when the regular expression
# is parsed. thus '\\w' becomes the literal search pattern "\w" within $a.

if ($a =~ /c:\\windows/i) {
print "did match $a to 'c:\windows'\n";
}

Be sure not to confuse yourself with the constructs you have listed.
It does not seam like you are distinguishing the string you want to test with
the regular expression you use to test it with.

sln



 
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
Help with Pattern matching. Matching multiple lines from while reading from a file. Bobby Chamness Perl Misc 2 05-03-2007 06:02 PM
Baffled =?Utf-8?B?UGFt?= Wireless Networking 21 01-22-2006 09:12 PM
Absolutely baffled - card not seeing network =?Utf-8?B?QW5ndXM=?= Wireless Networking 4 10-30-2005 10:04 PM
Baffled by Wireless Connection =?Utf-8?B?UGFt?= Wireless Networking 2 03-07-2005 01:13 AM
Pattern matching : not matching problem Marc Bissonnette Perl Misc 9 01-13-2004 05:52 PM



Advertisments