in reply to Find and Replace with special characters

The regex you showed us is using a backslash escape for "#" (which doesn't really need to be escaped), whereas you need escapes for open the parens. (Also, you're using a backslash in the replacement string, which would only be needed for getting a literal $ or @.)

And maybe it's just a typo in your post, but you seem to be using the =~ operator twice, which seems wrong - since you're modifying $_, you don't need it at all.

Yet another point: since you are only changing one character (# to *), you should use a "look-ahead assertion" for the other characters in the pattern.

Finally, to top it off, you put a backslash escape in front of the forward-slash character that was supposed to be the middle delimiter for the "s///' operator, so perl only sees two of the three forward-slashes as delimiters.

Try it like this:

foreach ( @lines ) { s/#(?=<-\(0N<-\(s3T)/*/g; push(@newlines,$_); }
(Update: Admittedly, using the look-ahead assertion in this case feels a bit uncomfortable, because you might get it confused with the look-behind syntax:
/(?<=preceding_part)target_part/

Replies are listed 'Best First'.
Re^2: Find and Replace with special characters
by wrog (Friar) on May 07, 2015 at 08:59 UTC
    You also want to make up your mind on whether @lines is being modified in place. If so, then you don't need @newlines at all and it's just
    s/#(?=<-\(0N<-\(s3T)/*/g foreach ( @lines );
    Otherwise, if you need to keep both versions around, then it's
    @newlines = map { s/#(?=<-\(0N<-\(s3T)/*/gr } @lines;
    (it being somewhat more efficient to allocate @newlines in one swell foop).