user3574070
user3574070

Reputation: 1

Perl Search and Replace — issues is caused by "\"

I am parsing a text doc and replacing some text. Lines of text without the "\" seem to be found and replaced no issues.

By the way this is to be done in Perl

I have a string like below:

Path=S:\2014 March\Test Scenarios\load\2014 March

that contains "\" that slash is an issue. I am using a simple search and replace line of code

$nExit =~ s/$sMatchPattern/$sFullReplacementString/; 

How should I do it?

Upvotes: 0

Views: 48

Answers (3)

David W.
David W.

Reputation: 107040

Is this string inputed, or is it embedded in your program. You could do this to get rid of the backslash character:

my $path = "S:/2014 March/Test Scenarios/load/2014 March";

By the way, it's best not to have spaces in file and path names. They can be a bit problematic in certain situations. If you can't eliminate them, it's understandable.

Two things you should look at:

  1. Use quotemeta which can help quote special characters in strings and allow you to use them in substitutions. Even if you had backslashes in your strings, quotemeta will handle them.
  2. You don't have to use / as separators in match and substitutions. Instead, you can substitute various other characters.

These are all the same:

$string =~ s/$regex/$replace/;
$string =~ s#$regex#$replace#;
$string =~ s|$regex|$replace|;

You can also use parentheses, square braces, or curly brackets:

$string =~ s($regex)($replace);
$string =~ s[$regex][$replace]; # Not really recommended because `[...]` is a common regex
$string =~ s{$regex}{$replace};

The advantage of these as regular expression quote-like characters is that they must be balanced, so if I had this:

my $string  = "I have (parentheses) in my string";
my $regex   = "(parentheses}";
my $replace = "{curly braces}";

$string = s($regex)($replace);
print "$string\n";   # Still works. This will be "I have {curly braces} in my string"

Even if my string contains these types of characters, as long as they're balanced, everything will still work.

For yours:

my $Path = 'S:\2014 March\Test Scenarios\load\2014 March';
$nExit = quotemeta $string;  #Quotes all meta characters...
$nExit =~ s($sMatchPattern)($sFullReplacementString); 

That should work for you.

Upvotes: 2

communications
communications

Reputation: 145

if you want to have a \ in your replacement string or match string dont forget to put another backslash in front of the backslash you want, as its an operator...

$sFullReplacementString = "\\";

That would turn the string into a single \

Upvotes: 1

Miller
Miller

Reputation: 35198

I suspect that you're trying to match a literal string, and therefore need to escape regex special characters.

You can use quotemeta or the escape codes \Q ... \E to do that:

$nExit = s/\Q$sMatchPattern/$sFullReplacementString/; 

The above variable $sMatchPattern will be interpolated, but then any special characters will be escaped before the regex is compiled. Therefore the value of $sMatchPattern will be treated like a literal string.

Upvotes: 3

Related Questions