back vs. forward slash 
Author Message
 back vs. forward slash

I thought I developed a cool little pattern match to read in relative paths
for header files but the match always sees forward slashes, not backslashes.

Can you see why from this code?:

-------------

$headerRelPath = '"..\\..\\header.h"';

if ($headerRelPath =~ /"([\.\.\\]+)(.*)"/i) {
    print ("forward slash found\n");

Quote:
} elsif ($headerRelPath =~ /"([\.\.\/]+)(.*)"/i) {

    print ("back slash found\n");
Quote:
}

------------------

THANKS!



Wed, 18 Jun 1902 08:00:00 GMT  
 back vs. forward slash
The barackets ([]) say that you can match any character in the
brackets. You have two periods and a backslash (\) in the first bracket
followed by a "+" which means "match any expression that contain either
one or more periods or one or more backslashes". Since all of your
expressions contain a period, they all will match the first expression.
I tried the same expression without the brackets, and it works:

$headerRelPath = '"../../header.h"';    #You can reverse the first
$headerRelPath = '"..\\..\\header.h"';  #two lines to check for "/"
print "$headerRelPath\n";

if ($headerRelPath =~ /"(\.\.\\)+(.*)"/) {
    print "backslashes used\n";

Quote:
}

elsif ($headerRelPath =~ /"(\.\.\/)+(.*)"/) {
    print "forward slashes used\n";
Quote:
}

else {
    print "Completely Failed\n";

Quote:
}

Notice that the parentheses are used to group the regular expression.
The "(\.\.\\)+" says to match a "..\" pattern one or more times which
is what you really want.

Now, I didn't thoroughly test this expression, and I would be leary of
using it as it is. For example, I might feel better anchoring the
expression either at the beginning of the line or the end of the line.
And, what happens if the developer used single quotes instead of double
quotes?



Quote:
> I thought I developed a cool little pattern match to read in relative
paths
> for header files but the match always sees forward slashes, not
backslashes.

> Can you see why from this code?:

> -------------

> $headerRelPath = '"..\\..\\header.h"';

> if ($headerRelPath =~ /"([\.\.\\]+)(.*)"/i) {
>     print ("forward slash found\n");
> } elsif ($headerRelPath =~ /"([\.\.\/]+)(.*)"/i) {
>     print ("back slash found\n");
> }
> ------------------

> THANKS!

Sent via Deja.com http://www.deja.com/
Before you buy.


Wed, 18 Jun 1902 08:00:00 GMT  
 back vs. forward slash

Quote:

> I thought I developed a cool little pattern match to read in relative paths
> for header files but the match always sees forward slashes, not backslashes.

> Can you see why from this code?:

> -------------

> $headerRelPath = '"..\\..\\header.h"';

You do know that NT handles slashes without problems, don't you?

Quote:
> if ($headerRelPath =~ /"([\.\.\\]+)(.*)"/i) {

Where is the forward slash? Your match is equivalent to
/"([.\\]+)(.*)"/
(match at least a dot or backwhack etc.)

Quote:
>     print ("forward slash found\n");
> } elsif ($headerRelPath =~ /"([\.\.\/]+)(.*)"/i) {

Where is the backslash? This is equivalent to
m|"([./]+)(.*)"|
It matches forward slashes

- Alex



Wed, 18 Jun 1902 08:00:00 GMT  
 back vs. forward slash


Quote:
> I thought I developed a cool little pattern match to read in relative paths
> for header files but the match always sees forward slashes, not backslashes.

> Can you see why from this code?:

I can see that you don't know the definitions.  This is a forward slash:
/; this is a backslash: \.

Quote:
> -------------

> $headerRelPath = '"..\\..\\header.h"';

> if ($headerRelPath =~ /"([\.\.\\]+)(.*)"/i) {

Perhaps you are also unaware of the meaning of square brackets in
regexes.  This regex looks for a double-quote followed by one or more
characters from the set 'dot' and 'backslash', ...

Quote:
>     print ("forward slash found\n");
> } elsif ($headerRelPath =~ /"([\.\.\/]+)(.*)"/i) {
>     print ("back slash found\n");
> }

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Changing back slashes to forward slashes

2. Erroneous Forward Slash in HTML Code by CGI.pm

3. Slash vs backslash in nt vs 95?

4. Substituting backslash for 2 forward slashes

5. X-Forwarded-for vs remote_addr vs remote_ip

6. Recognizing BACK and FORWARD button-visits

7. NT Perl 5 and slash vs. backslash

8. Forward slash in Win95 ?? & Perl on Win32

9. why [^\/] didn't work --- How to march non-forward slash?

10. why [^\/] didn't work --- How to march non-forward slash?

11. Mouse vs. Shortcut vs Mnemonics vs Tabbing:

12. Regex Variable String ends in slash

 

 
Powered by phpBB® Forum Software