验证用户输入 [英] Verifying User Input

查看:44
本文介绍了验证用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用户会在这样的地方输入一些字节.

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

  • 星号:匹配任意字节数的通配符
  • 问号:匹配任意单个字节的通配符

我必须检查

  • 有效字符(0-9、A-F、上/下)
  • 两个通配符?*
  • 正确放置逗号
  • 没有双逗号
  • 适当的空格(例如,数字之间没有空格)
  • 异常的空格(例如,他可以在逗号后放置零个、一个或多个空格)
  • 可能的单个字符表示一个字节(例如0"而不是00")

这里有一些例子..

(第一次编辑和更新,所有用户都不是平等的;语法检查器必须处理这个...)

来自聪明的好用户的漂亮的正常格式整齐的输入...

  • 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

来自愚蠢的坏用户的丑陋草率输入...

  • 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

(ps,愚蠢的坏用户有时会在尾随空格中放置丑陋的空格,有时在前导空格中放置丑陋的空格)

真遗憾,这变得毛茸茸的.我最多可以使用三个嵌套函数,而且离完成还差得很远.我以为这将是一个 20 分钟的打字练习.

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 ?

Visual Studio C# 是否已经包含一个属性来要求仅使用以逗号分隔的十六进制字节?

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 ?

StackOverflow 上自动提出的类似问题并没有真正回答这个问题,这让我感到惊讶.当然,我不是第一个遇到这种烦恼的人.欢迎提出建议

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);

正则表达式是 ^([0-9A-F]{2},\s?)*[0-9A-F]{2}$

^ - 开始
[0-9A-F] - 十六进制字符集
{2} - 该集合的两个字符
, - 只是逗号
\s?- 可选空间
* - 重复 0 次或更多次

^ - beginning
[0-9A-F] - hex characters set
{2} - two characters of that set
, - just comma
\s? - optional space
* - repeated 0 or more times

注意:如果要在逗号后强制使用空格,请使用 ^([0-9A-F]{2},\s)*[0-9A-F]{2}$

Note: if you want to enforce a space after the comma, use ^([0-9A-F]{2},\s)*[0-9A-F]{2}$

注2:如果只允许一个字符并允许小写字母,使用^([0-9A-Fa-f]{1,2},\s)*[0-9A-Fa-f]{1,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}$

这篇关于验证用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆