The Data::Validate module has a is_hex function for which the source looks like this:
sub is_hex { my $self = shift if ref($_[0]); my $value = shift; return unless defined $value; return if $value =~ /[^0-9a-f]/i; $value = lc($value); my $int = hex($value); return unless (defined $int); my $hex = sprintf "%x", $int; return $hex if ($hex eq $value); # handle zero stripping if (my ($z) = $value =~ /^(0+)/) { return "$z$hex" if ("$z$hex" eq $value); } return; }
Note that this module does not support the '0x' hexadecimal syntax, so you would have to strip that off yourself.
use warnings; use strict; use Data::Validate qw(:math); my @data = qw(1234 ABCD 0x1234 foo 0xABCD bar); foreach my $val (@data) { if (defined(is_hex($val))) { print "$val\tis hex"; } else { print "$val\tis not hex"; } } __END__
$ perl -l 629540.pl 1234 is hex ABCD is hex 0x1234 is not hex foo is not hex 0xABCD is not hex bar is not hex
--
Andreas

In reply to Re: Regular expression for hexadecimal number by andreas1234567
in thread Regular expression for hexadecimal number by isha

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.