Solved

\\ regex

Posted on 2002-05-14
3
239 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now