Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

[perlre] 4-digit hex code in regexp? How to specify?

by princepawn (Parson)
on Nov 14, 2002 at 21:01 UTC ( #213015=perlquestion: print w/replies, xml ) Need Help??

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

As you might be able to tell from the Basic Unicode Chart, a $ sign has hex value 0024. However, in Perl, I cannot write 0024, but must only write 24. This could problem problematic when dealing with a larger hex code than 2 digits. How can I specify a hex code in Perl using 4 digits instead of 2?
$delimiter = 'Y'; $_ = q(this has $100 worth of salt in it) ; warn $_; s/([\x0024])/$delimiter/gm; warn $_; s/([\x24])/$delimiter/gm; warn $_;

Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality

Replies are listed 'Best First'.
Re: [perlre] 4-digit hex code in regexp? How to specify?
by nothingmuch (Priest) on Nov 14, 2002 at 21:21 UTC
    \x{decaf} - Damn, that's five.

    Update: Retrospectively I think it's worth a mention that this is off the Camel Book, from one of the reference sections. Thanks for enlightening \msgs tho... =)

    zz zZ Z Z #!perl
Re: [perlre] 4-digit hex code in regexp? How to specify?
by hawtin (Prior) on Nov 14, 2002 at 21:38 UTC
    According to the docs the following should allow 4 byte Unicode defs:

    Update: Sorry did not spot that this had been dismissed. How about the clunky

    { my($temp); $temp1 = "\x{0024}"; $temp2 = "\x{1F45}"; s/([$temp1,$temp2])/$delimiter/gm; }
      Except you don't want the comma in the character class. And why use two variables?
      { my $temp = "\x{0024}\x{1F45}"; s/([$temp])/$delimiter/gm; }
      Which if often used might be better written as
      # somewhere in init code my $descriptive_charclass_name = do { my $wide = "\x{0024}\x{1F45}"; qr/[$widechars]/; }; # ... later s/($descriptive_charclass_name)/$delimiter/gm;

      Makeshifts last the longest.

Re: [perlre] 4-digit hex code in regexp? How to specify?
by Mr. Muskrat (Canon) on Nov 14, 2002 at 21:16 UTC

    This works great until you try to put it into brackets...

      There appear to be two ways to handle hex codes -- one is the one that you are using:


      The other is the method used by Mr. Muskrat:


      If you're using 'wide' hex digits (as I assume is the case since you're talking about four digits in this example) then you probably need to use the \x{...} approach.

      If you'll tell us a little more about what you're doing, we might be able to move beyond a single substitution. Unless that's all you need, in which case this appears to work:


      And you don't need to make it part of a character class

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://213015]
Approved by Mr. Muskrat
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2021-05-17 20:10 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (161 votes). Check out past polls.