Verifying User Input

怎甘沉沦 提交于 2019-12-13 15:18:28

问题


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...

  • A hex byte
  • Then a comma
  • Then white space (maybe, at his option)

Those three rules can be repeated as much as the user wants. For received bytes, there are two more rules to handle...

  • An Asterisk: wildcard which matches any number of bytes
  • A question mark: wildcard which matches any one single byte

I have to check for

  • Valid chars (0-9, A-F, upper/lower)
  • The two wild cards ? and *
  • Proper placement of commas
  • No double commas
  • Proper white space (e.g., none between digits)
  • Aberrant white space (e.g., he can put zero, one, or multiple spaces after a comma)
  • Possible single characters to represent a byte (e.g. "0" instead of "00")

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


回答1:


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}$




回答2:


If you are looking for a non regex solution, you can use this. Regex are very powerful. If you start programming, maybe it is not the best approach. Regex can be a mess to read after shot. Use what you want and have fun.

Warning! This snippet dont check wild cards. I didn't know what you want to do with '?' and '*'.

You can try my code in this Fiddle.

using System;

public class Program
{
    public static void Main()
    {
        bool b = checkValue("01, FF, 3E, 27, 7F");      // textBox.Text
        Console.WriteLine(b);
    }

    static bool checkValue(string s)
    {
        string[] tab = s.Split(new string[] { ", " }, StringSplitOptions.None ); // split string with ", " as a delimiter
        foreach (var hex in tab)
        {
            if (hex.Length != 2) // Check if we have two values
                return false;
            foreach(var c in hex)
            {
                // check for valid letter (upper and lower) and digit
                if(!((c >= '0' && c <= '9') || 
                    (c >= 'a' && c <= 'f') || 
                    (c >= 'A' && c <= 'F')))
                return false;
            }
        }
        return true;
    }
}

Just read that @CPU-Terminator think about the same solution then me.



来源:https://stackoverflow.com/questions/22773975/verifying-user-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!