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

hi can anyone tell me how to replace the following string?
First question:
my goal is to replace C:\Program Files\180searchassistant\salmhook.dll to a custom file name "C:\japansese characters\japnaese character filename.exe"
but because if \ the string replacement blows up. so I'm
thinking of replacing \ with /. However I can't do the replacement
globally because it will interfere with other field that contains \ (see source string) there are 2 places that hold filenames.
I want to replace "C:\Program Files\180searchassistant\salmhook.dll"
with "C:\Program Files\180searchassistant\salmhook.dll"

second question:
how to do replacement with the japanese character? file is in ASCII mode.
source string is read from a file in ASCII mode. source string:
23080E0D172F,5,1,102,JYPZ861,RBhagwat,Adware.180Solutions,C:\Program F +iles\180searchassistant\salmhook.dll,4,4,4,416,1124089892,"",11267290 +87,,0,,0,46122,0,0,0,0,0,0,20050911.006,48244,2,5,0,SVLSAV01,{221E42A +7-E23D-49E5-AF2C-2A22710FA5A8},SVL-Laptop,(IP)-192.168.1.2,US-West,EN +TERPRISE,,9.0.1.1000,,,0,0,,"description C:\123\3456\myfile",,,,,,,,,
any advice?

Replies are listed 'Best First'.
Re: string replacement question
by GrandFather (Saint) on Dec 08, 2005 at 02:15 UTC

    Show us the snippet of code that you are using at present. Also, reduce the length of your test string to just sufficient to demonstrate the problem.


    DWIM is Perl's answer to Gödel
Re: string replacement question
by secret (Beadle) on Dec 08, 2005 at 09:24 UTC
    Hello,

    You say :

    I want to replace "C:\Program Files\180searchassistant\salmhook.dll" with "C:\Program Files\180searchassistant\salmhook.dll"

    But these two string are identical ?!
    As i understand it you have a problem with the backslash. It's not a real problem : escape the backslash

    # note the use of single quotes $string = 'c:\foo\bar\weird program name.exe' ; # using another regexp separator for readability $string =~ s[foo\\bar][foo\\baz] ; # Or with / as the separator $string =~ s/foo\\bar/foo\\baz/ ;
    Well, you can do this a gazillion ways there's no problems with the backslash ...

    Now about replacing japanese characters with a regexp, as i understand this will call for unicode regexp. Supersearch will give you some result.

      But, pedantically, note the initial phrasing of OP's post DOES appear to specify a replacement:
      my goal is to replace C:\Program Files\180searchassistant\salmhook.dll to a custom file name "C:\japansese characters\japnaese character filename.exe"
      .

      nonetheless, ++ for the portion of your post re the backslash...

      and re OP's problem with multiple instances of "However I can't do the replacement globally because ... there are 2 places that hold filenames. " one might suggest that:

      • if OP knows the replaceable-instance always comes first, a simple regex, applied NON-globally (ie, *WITHOUT* a /g) should do the trick
      • if placement of the replaceable-instance is unknown or variable, my first thought would be to see if some other defining characteristic (context, for example) might be used in the regex
      FWIW....
      ww