The reason your substitution isn't working is because you aren't escaping the first backslash \x probably has a special meaning and even if it doesn't \x means x (backslashed \> for example means >).

To fix the regex, rewrite it like this:

s/\\x(\d+)/'&#' . hex($1) . ';'/ge
The \d+ will work as you think it should (that is, as you wrote it).

Scott

Update: I should have guessed, but the \x followed by a two digit hex number means to look for a hex number, which is probably what confused you in the first place: you are looking for the text representation of a hex number (ie, \x92) which is plain text, not the hex number itself.

The Real Update: OK, you said For instance, the single character \x92; scain, read the problem (or as my High School Chemistry teacher wrote RTGDP). \x followed by digits matches a hex nubmer. What I don't know is how to get it to match a range of numbers. You might try several things, like [\x90-\xFF], which if Perl DWIM, would mean to match any hex number in that range. Also, since \x actually matches the number, what you really want to capture is the whole thing; so assuming the range works as I have written it, rewrite the regex like this:

s/([\x90-\xFF])/'&#' . $1 . ';'/g
I am really fuzzy on whether that will work, but don't have an easy way to test it; let me know. Note that this way, you no longer need the e at the end of the regex.

In reply to Re: Control Characters (\xNN) in HTML by scain
in thread Control Characters (\xNN) in HTML by garliqua

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.