Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

\\ regex

Posted on 2002-05-14
3
Medium Priority
?
250 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 200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Six Sigma Control Plans

963 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