By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,910 Members | 1,085 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,910 IT Pros & Developers. It's quick & easy.

RegEx for Hex string validation

P: n/a
slg
How can i validate the characters in a string are all hex chars.

I tried following but it does not work.

Regex r = new Regex(@"^([A-F]|[a-f]|[0-9])*");

TIA.

Dec 1 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi,

what about trying to convert the hex into a e.g. long value?
When it fails you will get a exception and you can handle the
exception depending on the error.

Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Microsoft Live Space: http://kerem-g.spaces.live.com/
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
Dec 1 '07 #2

P: n/a
slg <sl*@abc.comwrote:
How can i validate the characters in a string are all hex chars.

I tried following but it does not work.

Regex r = new Regex(@"^([A-F]|[a-f]|[0-9])*");
You've got the ^ at the beginning, but no $ at the end. You can also
express the middle part more simply:

Regex r = new Regex(@"^[A-Fa-f0-9]*$");

Note that it will match an empty string as well as one with actual
content - is that what you want?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 1 '07 #3

P: n/a
slg
thx alot

Yes, this is exactly what i was looking for.

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
slg <sl*@abc.comwrote:
> How can i validate the characters in a string are all hex chars.

I tried following but it does not work.

Regex r = new Regex(@"^([A-F]|[a-f]|[0-9])*");

You've got the ^ at the beginning, but no $ at the end. You can also
express the middle part more simply:

Regex r = new Regex(@"^[A-Fa-f0-9]*$");

Note that it will match an empty string as well as one with actual
content - is that what you want?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 1 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.