Solved

\\ regex

Posted on 2002-05-14
3
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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

632 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