$line is still encoded. A character won't match the UTF-8 encoding of that character unless it's an ASCII character.

While that may be an accurate statement, trying to decipher what it means is not easy.

Here is how I would put it: a unicode character is not the same as a unicode character encoded in UTF-8. There are many encodings, and UTF-8 is only one of them. However, there is only one unicode character for the copyright symbol. Simply put, if you want to match UTF-8 characters in a string, then you need to use UTF-8 characters in your substitution--not unicode characters.

Here is a code example:

use strict; use warnings; use 5.010; use Encode; my $unicode_str = "\x{00a9}"; my $utf8_str = encode('utf-8', $unicode_str); say $utf8_str; #copyright symbol my $line = "$utf8_str hello world"; $line =~ s/$utf8_str/\\textcopyright/; say $line; #\textcopyright hello world #Or you can just start with the UTF-8 character #for the copyright symbol: $line = "\xC2\xA9 hello world"; say $line; #copyright symbol followed by 'hello world' $line =~ s/\xC2\xA9/\\textcopyright/; say $line; #\textcopyright hello world

In my opinion, the easiest way to understand the whole unicode thing is this: a unicode escape sequence is an integer. An 'encoding' converts a unicode integer into a character. An encoding is just a list that looks like this:

1 => chinese character for the new year 2 => japanese character for fish 3 => happy face ... ... 60,000 => mongolian character for beef ...

So an encoding takes unicode integers and translates them into characters. Different encodings translate the unicode integers into different characters. UTF-8 is just one encoding, which is very popular.


In reply to Re: Search & replace of UTF-8 characters ? by 7stud
in thread Search & replace of UTF-8 characters ? by levien

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.