My user will enter some bytes in a place like this.
Yellow arrow points to his input, Orange arrow points to my button
After about half an hour, I began to realize that this is far more tedious than I expected.
Question: Am I going to have to write hundreds of lines of code to ensure that the user follows the rules ?
The rules for the syntax in his input are...
Those three rules can be repeated as much as the user wants. For received bytes, there are two more rules to handle...
I have to check for
?
and *
Here are some examples..
(First edit and update, all users are not created equal; the syntax checker must handle this...)
Nice Normal Neatly Formatted Input From Smart Good Users...
01, FF, 3E, 27, 7F
55, EE, 01, 00
21, FE, 2B, 00, 1F
37, *, 18, ?, 00, 37
81, *, 00, *, FF, 91, ?, 11, ?, FF
20, 31, 7F, 28, *, FF
47 4F, 20, 50, 4F, 4D, 45, ?, 21
Ugly Sloppy Input From Stupid Bad Users...
1,ff,3e,27,7f
55, EE, 1, 00
21,Fe, 2b,0, 1f
37,*,18,?,00,37
81, *, 0, *,Ff, 91,?,11, ?,FF
20, 31, 7f, 28, *, FF
47, 4F, 20, 50,4F,4D,45, ?,21
(p.s., Stupid Bad Users sometimes put in ugly trailing spaces, and sometimes ugly leading spaces)
Good grief this is getting hairy. I'm up to three nested functions and I'm nowhere close to finished. I thought it was going to be a 20 minute typing exercise.
Has this problem already been solved before ?
Does Visual Studio C# already contain a property to demand only hex bytes separated by commas ?
Did I miss the obvious tree in the forest ?
The similar questions which were automatically suggested here on StackOverflow didn't really answer this, which surprises me. Certainly I'm not the first one to hit this annoyance. Suggestions welcome
You can use regular expressions to do that:
String input = "01, FF, 3E, 27, 7F";
String pattern = @"^([0-9A-F]{2},\s?)*[0-9A-F]{2}$";
bool matches = Regex.IsMatch(input, pattern);
The regex is ^([0-9A-F]{2},\s?)*[0-9A-F]{2}$
which is
^ - beginning
[0-9A-F] - hex characters set
{2} - two characters of that set
, - just comma
\s? - optional space
* - repeated 0 or more times
Note: if you want to enforce a space after the comma, use ^([0-9A-F]{2},\s)*[0-9A-F]{2}$
Note 2: if you want to allow only one character and allow lower case letters, use ^([0-9A-Fa-f]{1,2},\s)*[0-9A-Fa-f]{1,2}$