Solved

\\ regex

Posted on 2002-05-14
3
241 Views
Last Modified: 2010-03-05
Hi, my regex is just poor, please help me out here

I want to make sure my $string is double slashed as path separator, my $string can be

$string = "C:\temp\folder"
or
$string = "C:\\temp\\folder"

I have this
$string =~ s/\\/\\\\/g;
but it's wrong because the second $string will become
$string = "C:\\\\temp\\\\folder"

How to specify one \ only?

Thanks.
0
Comment
Question by:kitwei
3 Comments
 

Expert Comment

by:iddo_levin
ID: 7009381
Should work perfectly:

$string =~ s/\\(?!=\\)/\\\\/g;
0
 

Author Comment

by:kitwei
ID: 7009420
Hm... nope, I still get 4 \
0
 
LVL 6

Accepted Solution

by:
sstouk earned 50 total points
ID: 7009456
Here:
$string1 = 'C:\temp\folder';
$string2 = 'C:\\temp\\folder';

$string1 =~ s!\\\\*!\\\\!g;
$string2 =~ s!\\\\*!\\\\!g;
print "$string1\n$string2\n";
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question