Solved

\\ regex

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl 101 11 85
stftime format 4 58
Bash one liner, start and end time calculation 13 120
Control Number of Log Files -Perl 7 94
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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

713 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