I have a string which is formatted like this: $20,$40,$AA,$FF. Basically, hex numbers and they can be of many bytes. I want to check if a string is in the above format, so I tried something like this:
string a = "$20,$30,$40";
Regex reg = new Regex(@"$[0-9a-fA-F],");
if (a.StartsWith(string.Format("{0}{1}", reg, reg)))
MessageBox.Show("A");
It doesn’t seem to work though, is there anything I’m missing?
There are 3 things that need to be changed:
Need to escape your $ symbol as it represents end of line.
Need to tweak your regex pattern to match the entire string instead of parts.
Need to change your code to use Regex.IsMatch.
PS:
If the input string has white space like a tab or a space in between, then this regex will need to be modified. In such cases, you have to use “\s” at the right positions. For example, if you have white space around the commas like
then you need to tweak your RegEx this way:
References:
Old answer below (Ignore):
Try this: