Upendra has asked for the wisdom of the Perl Monks concerning the following question:

Dear All, thank you for reading my post, hopefully someone can shed some light on my issue, I am parasing a txt doc and replacing some text. Lines of text with out the "\" seem to be found and replaced no issues

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/;

any thoughts

Thanks in Advance Upendra

Replies are listed 'Best First'.
Re: Search and Replace
by rjt (Curate) on Apr 25, 2014 at 18:28 UTC

    By the sounds of your description, $sMatchPattern probably contains literal text rather than an actual regular expression pattern, or at least contains a literal \.

    If you are searching for literal text, use the \Q and \E escapes to tell the regular expression engine to leave the text alone:

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

    You also might be running into problems with escapes in your strings (which we could easily spot if you show your code!) For example:

    my $oops = "\tell me something"; # <TAB>ell me something my $ok = '\tell me something'; # tell me something
    use strict; use warnings; omitted for brevity.
Re: Search and Replace
by AnomalousMonk (Archbishop) on Apr 25, 2014 at 18:25 UTC

    Saying that something is "an issue" is even less informative than saying it "doesn't work." Please see I know what I mean. Why don't you?.

    One potential "issue" associated with backslashes is that they are used to introduce escape sequences in double-quoted strings. (The  qq{...} in the example below is a double-quote like  "..." used because Windows quotes command-line strings with "s. Please see Quote and Quote-like Operators in perlop.)

    c:\@Work\Perl\monks>perl -wMstrict -le "print qq{hi \there \ned!}; print 'bye \there \ned.'; " hi here ed! bye \there \ned.
Re: Search and Replace
by Laurent_R (Canon) on Apr 25, 2014 at 17:53 UTC
    Please provide more of your code. We would at least need to know what you have in the $sMatchPattern and $sFullReplacementString variables. Oh, and please use code tags for code: <c> and </c>