sajis997
sajis997

Reputation: 1171

regular expression for hexadecimal

I have defined a regular expression of hexadecimal value of length 4 as follows:

([0-9A-F]{4})

And it works fine and grammatically

0000

is also a valid hexadecimal number , but I want to discard it from the valid matching and therefore looking forward to hint on how to extend the current regular expression so that

0000 is flagged as invalid and filtered out.

Thanks

Upvotes: 9

Views: 2829

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476967

You could use negative lookahead:

(?!0000)[0-9A-F]{4}

Here (?!0000) is a negative lookahead group. It more or less says: "And do not allow that the next elements are 0000" but without consuming them.

You can test it on regex101.

Upvotes: 13

Related Questions