CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate: Re: creating directory list?: Edit Log



mhx
Enthusiast / Moderator

Jun 22, 2001, 7:21 PM


Views: 945
Re: creating directory list?

Hi,

I think you got me wrong. I wasn't saying you were looking only for the last character. What your regex does is look for the $path with the last character of $path being optional instead of the whole $path. Perhaps it's because I'm no native english speaker. Anyway, here's an example to emphasize my concern:

Code
#!/bin/perl -w 
use strict;

my $path = '/path/to/files';
my $other_path = '/another/path';

my $file = 'myfile';

my $test = "$path/$file";
my $other_test = "$other_path/$file";


# your original regex

print $test =~ /^$path?(.*)$/ # /myfile
? "$1\n" : "no match\n";
print $other_test =~ /^$path?(.*)$/ # no match
? "$1\n" : "no match\n";


# your regex without the ?

print $test =~ /^$path(.*)$/ # /myfile
? "$1\n" : "no match\n";
print $other_test =~ /^$path(.*)$/ # no match
? "$1\n" : "no match\n";


# my modified version of your regex

print $test =~ /^(?:$path)?(.*)$/ # /myfile
? "$1\n" : "no match\n";
print $other_test =~ /^(?:$path)?(.*)$/ # /another/path/myfile
? "$1\n" : "no match\n";

As far as I understand, you want to strip of the $path from the beginning of the string. But if the string doesn't begin with $path, you don't want to strip off anything.
If you look at the three regexes above, only my modified variant does this. For any other behaviour, I guess, the ? is completely useless, as you can see if you compare the first two regexes. It's useless because

In Reply To
Also, when a scalar is tossed into a regex, ? isn't looking for just the last character of the scalar's value -- it's looking for the entire value of the scalar.

is wrong. First, $path is interpolated, then the resulting string is passed to the regex engine. You can find this illustrated in Jeffrey Friedl's 'Mastering Regular Expression'. As a proof that your original regex has only the last character of $path optional, I've included the debug output of the regex engine for your original regex

Code
Compiling REx `^/path/to/files?(.*)$' 
size 18 first at 2
1: BOL(2)
2: EXACT </path/to/file>(7) <- exact match
7: CURLY {0,1}(11) <- {0,1} means optional
9: EXACT <s>(0) <- 's' is optional
11: OPEN1(13)
13: STAR(15)
14: REG_ANY(0)
15: CLOSE1(17)
17: EOL(18)
18: END(0)

and for my modified one

Code
Compiling REx `^(?:/path/to/files)?(.*)$' 
size 18 first at 2
1: BOL(2)
2: CURLYM[0] {0,1}(11)
4: EXACT </path/to/files>(9) <- this is optional
9: SUCCEED(0)
10: NOTHING(11)
11: OPEN1(13)
13: STAR(15)
14: REG_ANY(0)
15: CLOSE1(17)
17: EOL(18)
18: END(0)

Do you understand what I'm trying to express? Sorry if I cannot explain this understandable enough for you.

-- Marcus



(This post was edited by mhx on Jun 22, 2001, 6:22 PM)


This post has not been edited


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives