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

I use an external programs to extract symbols from an object file. I need to do a perl script (or so seems it) which will replace stuff by other stuff in a binary file, the problem being the stuff part.

I call the external program which gives me the list of symbols to replace (and their replacements can be easily computed in the script). Those symbols may contains characters like . [ { ] } ( and others.

I've searched on the web for more than 10 hours straight, and haven't found a way of doing $SomeStuff =~ s/$match/$replace/g while having special characters in the $match variable. Nor there seems to be any other way of doing it. I don't even know if I can do it in Perl.

I'm exhausted and I want to sleep and be over with it :P. So please help me. Your karma will be rewarded.

Thanks in advance,
Hans :)

Replies are listed 'Best First'.
Re: Search and Replace fears
by reasonablekeith (Deacon) on Jul 11, 2005 at 14:52 UTC
    is this what you want?
    my $string = "["; print "\Q$string";
    ---
    my name's not Keith, and I'm not reasonable.
Re: Search and Replace fears
by reasonablekeith (Deacon) on Jul 11, 2005 at 15:31 UTC
    As an aside, and at the risk of monopolising this thread, you might also want to take a look at 'index'.
    perldoc -f index index STR,SUBSTR,POSITION index STR,SUBSTR The index function searches for one string within another, but without the wildcard-like behavior of a full regular-expression pattern match. It returns the position of the first occurrence of SUBSTR in STR at or after POSITION. If POSITION is omitted, starts searching from the beginning of the string. The return value is based at "0" (or whatever you've set the "$[" variable to--but don't do that). If the substring is not found, returns one less than the base, ordinarily "-1".
    ---
    my name's not Keith, and I'm not reasonable.
Re: Search and Replace fears
by jhourcle (Prior) on Jul 11, 2005 at 14:57 UTC

    You just need to escape the characters with a \. Of course, that's also a special character...

    my $match = '(some [very] odd value).'; my $replace = '(modified value)'; # escape anything that might be a potential problem. # note -- we ignore known good values, rather than # encoding known bad values (so if we don't know about, # it gets encoded) $match =~ s/([^\w\s])/\\\1/g; my $regex = qr/$match/; my $SomeStuff = 'This is a test string (value); (some [very] odd value +).'; $SomeStuff =~ s/$regex/$replace/g; print $SomeStuff;

    Update: Sorry, I didn't know about \Q ... take a look at reasonablekeith's response. (the logic from mine might be useful if you're trying to force some alternate escape sequence, if there's a possibility of wanting to escape some characters, but allow some to pass through untouched).

      You don't need to do all this, it's what \Q is for!

      Perhaps I should have been more explicit...

      my $string = 'abcdef'; my $match = '[a-z]'; $string =~ s/\Q$match/NOT REPLACED/; print "$string\n"; $string =~ s/$match/REPLACED/; print "$string\n"; __OUTPUT__ abcdef REPLACEDbcdef
      have a look in ...
      perldoc perlre
      ---
      my name's not Keith, and I'm not reasonable.