I am total noob regarding regex
.
My goal is to check wether a string is a valid represantion of a HEX number.
Currently my implementation (which I find really un-efficient) is having a List with all HEX digits (0,1,...9,A,B..F) and check wether my string contains chars not contained in given List.
I bet this can be easily done using regular expressions but I have no Idea how to implement it.
private bool ISValidHEX(string s)
{
List<string> ToCheck = new List<string>();
for (int i = 0; i < 10; i++)
{
ToCheck.Add(i.ToString());
}
ToCheck.Add("A");
ToCheck.Add("B");
ToCheck.Add("C");
ToCheck.Add("D");
ToCheck.Add("E");
ToCheck.Add("F");
for (int i = 0; i < s.Length; i++)
{
if( !ToCheck.Contains(s.Substring(i,1)))
{
return false;
}
}
return true;
}
See Question&Answers more detail:os