Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows backslash and regular expression 2

Status
Not open for further replies.

denisl

Technical User
Jun 18, 2001
36
US
I'm trying to find a directory path in a file and can only get it to work with 3 back slashes.

sample file:
Code:
this is a sample file line
here is C:\directory\match
another line

script to find the directory path named in the file:
Code:
$path = "C:\\\directory\\\match";
open IN, "samplefile" or die "failed" ;
undef $/;
$wholefile = (<IN>);

if ( $wholefile =~ /$path/ ) {
print "found it $path in file";
}

Why do I need 3 back slashes in the $path variable? I would think 2 back slashes would do the trick but it doesn't work.

Thanks.
Denis
 
Actually, 3 blackslashes would not work, you would need 4 in this instance. Your problem arises because the string is being interpolated twice. Once in your setting of $path because you're using a double quoted string, and a second time when the regular expression is compiled.

There are many ways to make this work in a way that you want and hopefully make sense as well. Personally though, I think the best is to explicitly stop interpolation using a single quoted string in your assignment, and then escaping the regular expression values because you want the characters in that string treated as literals, and not special characters.

Code:
#!/usr/bin/perl

$path = 'C:\directory\match';

local $/;
$wholefile = <DATA>;

if ( $wholefile =~ /\Q$path\E/ ) {
	print "found it $path in file";
}

1;

__DATA__
this is a sample file line
here is C:\directory\match
another line
 
Windows don't care if you use forward or backslashes. Use forward slashes to make life easy:


$path = 'C:/directory/match';

this avoids all sorts of problems with interpolation of stuff like \d which is the character class for digits.


- Kevin, perl coder unexceptional!
 
Kevin, that won't work

He is specifically trying to match a path string located within a file. You are correct in that perl treats forward slashes like black slashes for file operation purposes. But since he is trying to match a literal string, he has to keep the backslashes in this instance. At least given how he's described his problem.
 
MillerH - like you said, the back slashes are patterns in the file so your solution looks like the way to go.

I have one more question.. You've showed me a 2 new RE operators \Q and \E. I read in perldoc perlfaq6 but I don't fully understand its need. Can you explain or maybe direct me to a good link on RE's?

Thanks for you help.
 
Thank you. You've been a great help.
 
>> Kevin, that won't work

[cannon] -> details


[spin]




- Kevin, perl coder unexceptional!
 
OMG Kevin! You know the best emoticons.

Have a star for making me laugh [afro2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top