Some random responses...

do you dispute that there is a left-curly (and a right-curly) in the \x{A3f4} string? What else would you call it/them?
I do not dispute that, so this string itself never represents a left curly brace, rather it has a left curly brace in it.

Oh, so you were thinking that "\x{A3f4}" when compiled double-quotishy into a string and then printed should print a left-curly! I follow you a little better now. My terminal is not configured for Unicode (as I assume this character to be) right now, so I cannot confirm what it will print, and I'm reluctant to launch myself into Unicode-land on-line to find out. However, I agree that the escape sequence \x{A3f4} when compiled double-quotishly (e.g., "ab\x{A3f4}cd") will compile to some character. But the single-quote-compiled string '\x{A3f4}' will always be literally \x{A3f4} and nothing else.

It's important to understand how backslashes (escapes) are compiled in single- and double-quoted strings. Consider the following:

Win8 Strawberry 5.8.9.5 (32) Tue 06/07/2022 12:17:53 C:\@Work\Perl\monks >perl use strict; use warnings; print '-\-\\-\\\-\\\\-\\\\\-\\\\\\-\\\\\\\-\\\\\\\\-'; ^Z -\-\-\\-\\-\\\-\\\-\\\\-\\\\-
Why do '\\\\\\\' and '\\\\\\\\' (7 and 8 backslashes, respectively) both compile to and print as four backslashes? How would this be different if compiled as a double-quoted string?

DB<1> $str2='\\x{263}'

This compiles to (and prints) the literal string \x{263} or literal-backslash, literal-lowercase-x, literal-left-curly, literal-2, literal-6, literal-3, literal-right-curly. The hex built-in cannot interpret a string in this format (and so returns zero (update: and a warning)), but can in "proper" format:

Win8 Strawberry 5.8.9.5 (32) Tue 06/07/2022 22:09:02 C:\@Work\Perl\monks >perl use strict; use warnings; my $h1 = 'A3f4'; my $h2 = 'xA3f4'; print hex 'A3f4', "\n"; print hex $h1, "\n"; print hex 'xA3f4', "\n"; print hex $h2, "\n"; print hex '\xA3f4', "\n"; print hex '\x{A3f4}', "\n"; ^Z 41972 41972 41972 41972 Illegal hexadecimal digit '\' ignored at - line 13. 0 Illegal hexadecimal digit '\' ignored at - line 14. 0

    DB<10> $str4='\x{aF}'
...
How do I tease 175 out of $str4?

We know that \x{aF} will not be interpreted by hex as a hex number. One way to extract the hex substring:

Win8 Strawberry 5.8.9.5 (32) Tue 06/07/2022 22:25:09 C:\@Work\Perl\monks >perl use strict; use warnings; my $str = '\x{aF}'; $str =~ m{ \A \\ x \{ ([[:xdigit:]]+) \} \z }xms; my $hex_digits = $1; print ">$hex_digits< \n"; my $hex_number_in_decimal = hex $hex_digits; print "$hex_number_in_decimal \n"; ^Z >aF< 175

Update: Another approach:

Win8 Strawberry 5.8.9.5 (32) Sat 06/11/2022 15:18:47 C:\@Work\Perl\monks >perl use strict; use warnings; my $str = '\x{aF}'; my ($hex_digits) = $str =~ m{ [[:xdigit:]]+ }xmsg; my $hex_number_in_decimal = hex $hex_digits; print "'$hex_digits' == $hex_number_in_decimal decimal \n"; ^Z 'aF' == 175 decimal
This approach can be useful when a string or record has been "validated" as to its structure and you know that certain substrings or fields are unambiguously present: these substrings/fields can then be easily and quickly extracted. Note the /g modifier on the m// match.


Give a man a fish:  <%-{-{-{-<


In reply to Re^7: Unescaped left brace in regex is passed through in regex by AnomalousMonk
in thread Unescaped left brace in regex is passed through in regex by gzh

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.