如何检查用户输入以获得正确的格式(How to check user input for correct formatting)

这是我到目前为止所提出的

private void CheckFormatting() { StringReader objReaderf = new StringReader(txtInput.Text); List<String> formatTextList = new List<String>(); do { formatTextList.Add(objReaderf.ReadLine()); } while (objReaderf.Peek() != -1); objReaderf.Close(); for (int i = 0; i < formatTextList.Count; i++) { } }

它的目的是检查用户是否以这种格式输入了他们的信息Gxx:xx:xx:xx JGxx其中“x”可以是任何整数。

如您所见,用户将其数据输入到多行文本框中。 然后,我将该数据输入列表中。 下一部分是我被困的地方。 我创建一个for循环逐行遍历列表,但我想我还需要逐个字符地遍历每一行。 我该怎么做呢? 或者有更快的方法吗?

提前致谢

This is what i've come up with so far

private void CheckFormatting() { StringReader objReaderf = new StringReader(txtInput.Text); List<String> formatTextList = new List<String>(); do { formatTextList.Add(objReaderf.ReadLine()); } while (objReaderf.Peek() != -1); objReaderf.Close(); for (int i = 0; i < formatTextList.Count; i++) { } }

What it is designed to do is check that the user has entered their information in this format Gxx:xx:xx:xx JGxx where "x" can be any integer.

As you can see the user inputs their data into a multi-line textbox. i then take that data and enter it into a list. the next part is where i'm stuck. i create a for loop to go through the list line by line, but i guess i will also need to go through each line character by character. How do i do this? or is there a faster way of doing it?

thanks in advance

最满意答案

尝试这个。

if (!System.Text.RegularExpressions.Regex.IsMatch("your_text", "G[0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{2} JG[0-9]{2}")) { //Error! }

Try this.

if (!System.Text.RegularExpressions.Regex.IsMatch("your_text", "G[0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{2} JG[0-9]{2}")) { //Error! }

更多推荐