Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.

replace string

by sandy1028 (Sexton)
on May 18, 2009 at 08:18 UTC ( [id://764597] : perlquestion . print w/replies, xml ) Need Help??

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

Hi, I have to read the file and if I find the characters like &ldquo,&rdquo,&rsquo I have to replace it with some characters.
$data =~ s/“/“/g; # change the lines $data =~ s/”/”/gi; $data =~ s/’/’/gi;
But this is not replaced in the file when it occurs. How to do this

Replies are listed 'Best First'.
Re: replace string
by CountZero (Bishop) on May 18, 2009 at 08:26 UTC
    Do you want to replace the actual characters or their HTML-entities?

    One possible reason for the failure could be that you are trying to replace the actual characters but your are looking for them by using their HTML-entities names; or the character codes you are checking for are not the same as those used in your file (different codepage).


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: replace string
by moritz (Cardinal) on May 18, 2009 at 08:27 UTC

    Please take a look at HTML::Entities, it might help you doing what you want in a more general way.

Re: replace string
by Utilitarian (Vicar) on May 18, 2009 at 08:30 UTC
    Could you attach a sample of your source file, the regexes you have in place should work as expected, provided the characters in the file are as you expect.
      How to convert XHTML entity characters to XML form
        No need, XHTML is XML
Re: replace string
by Anonymous Monk on May 18, 2009 at 08:21 UTC
    “ is made of 7 characters, you probably only have one