Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

[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... =)

    -nuffin
    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:
    s/([\x{1F24}])/$delimiter/gm;

    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...
    s/(\x{0024})/$delimiter/gm;

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

      \x1B

      The other is the method used by Mr. Muskrat:

      \x{263a}

      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:

      s/\x{0024}/$delimiter/

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2021-05-16 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (152 votes). Check out past polls.

    Notices?