user1216216
user1216216

Reputation: 601

Validate hexadecimal string using regular expression

I am validating a string whether it is hexadecimal or not using regular expression.

The expression I used is ^[A-Fa-f0-9]$. When I using this, the string AABB10 is recognized as a valid hexadecimal, but the string 10AABB is recognized as invalid.

How can I solve the problem?

Upvotes: 41

Views: 61881

Answers (5)

Yuushi
Yuushi

Reputation: 26040

You most likely need a +, so regex = '^[a-fA-F0-9]+$'. However, I'd be careful to (perhaps) think about such things as an optional 0x at the beginning of the string, which would make it ^(0x|0X)?[a-fA-F0-9]+$.

Upvotes: 57

Hashim Aziz
Hashim Aziz

Reputation: 6062

To more directly and comprehensively answer the original question, [A-Fa-f0-9] is a character class - character classes matches one character by default, and you need to follow them with a quantifier to make them match more.

+ is a quantifier that means match one or more of the given characters, so:

^[A-Fa-f0-9]+$

...would match any string of at least one hex character, which is what the OP wanted.

Apart from + (match at least one of the given characters) and * (match any number of the given characters), you can also use numbers as quantifiers.

To match a hexadecimal string that is exactly 8 characters:

^[A-f0-9]{8}$

To match a hexadecimal string that is 8 characters or more:

^[A-f0-9]{8,}$

To match a hexadecimal string that is between 8 to 32 characters:

^[A-f0-9]{8,32}$

Note that all of the above will match whole lines due to the line anchors ^ and $ - to match hex strings anywhere in a line, simply remove the anchors:

[A-Fa-f0-9]+

Upvotes: 0

Kartik Javali
Kartik Javali

Reputation: 327

If you want to process a hex dump such as,

0x01 0x02 0x03 0xff 0xFF

you may use split string using str.Text.Split(' '); to obtain a list, then you may iterate over the list using following pattern

^(0[x]){1}[a-fA-F0-9]{2}$

Upvotes: 0

Preet Kukreti
Preet Kukreti

Reputation: 8607

^[A-Fa-f0-9]+$

should work, + matches 1 or more chars.

Using Python:

In [1]: import re

In [2]: re.match?
Type:       function
Base Class: <type 'function'>
String Form:<function match at 0x01D9DCF0>
Namespace:  Interactive
File:       python27\lib\re.py
Definition: re.match(pattern, string, flags=0)
Docstring:
Try to apply the pattern at the start of the string, returning
a match object, or None if no match was found.

In [3]: re.match(r"^[A-Fa-f0-9]+$", "AABB10")
Out[3]: <_sre.SRE_Match at 0x3734c98>

In [4]: re.match(r"^[A-Fa-f0-9]+$", "10AABB")
Out[4]: <_sre.SRE_Match at 0x3734d08>

Ideally You might want something like ^(0[xX])?[A-Fa-f0-9]+$ so you can match against strings with the common 0x formatting like 0x1A2B3C4D

In [5]: re.match(r"^(0[xX])?[A-Fa-f0-9]+$", "0x1A2B3C4D")
Out[5]: <_sre.SRE_Match at 0x373c2e0>

Upvotes: 12

kino lucky
kino lucky

Reputation: 1405

Do you forget the '+'? Trying "^[A-Fa-f0-9]+$"

Upvotes: 1

Related Questions