Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#输入验证正则表达式_C#_Regex_Winforms - Fatal编程技术网

C#输入验证正则表达式

C#输入验证正则表达式,c#,regex,winforms,C#,Regex,Winforms,我刚开始学C。对不起,我的问题是noob 我的第一个培训应用程序是一个输入年龄并在消息框中输出的应用程序 我想用正则表达式验证输入,这样输入字母就会引起错误 问题是我不能让它接受正则表达式 private void textBox1_TextChanged(object sender, EventArgs e) { string age; age = textBox1.Text; }

我刚开始学C。对不起,我的问题是noob

我的第一个培训应用程序是一个输入年龄并在消息框中输出的应用程序

我想用正则表达式验证输入,这样输入字母就会引起错误

问题是我不能让它接受正则表达式

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string age;
            age = textBox1.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string regexpattern;
            regexpattern = "^\t+";
            string regex1;

            regex1 = Regex.IsMatch(regexpattern);

            if (textBox1.Text == regex1)
            {             
                MessageBox.Show("error, numbers only please!");
            }         
            else
            {
                string age;
                string afe;
                string afwe2;

                afe = "You are ";
                age = textBox1.Text;
                afwe2 = " years old!";

                MessageBox.Show(afe + age + afwe2);
            }
        }
谢谢

您的正则表达式必须是

regexpattern = "^\d+$"; 
编辑 而且编码是错误的。必须是这样的:

var regex = new Regex(@"^\d+$");

if (!regex.IsMatch(textBox1.Text))
{
    MessageBox.Show("error, numbers only please!");
}

对于任何开发人员来说,regex库都是一个很好的资源。很可能你要找的东西已经贴在那里了。例如,您可能希望将年龄限制在一定范围内


您不需要正则表达式,只需检查它是否是一个数字: 下面是一个示例代码,希望它能正常工作

private void button1_Click(object sender, EventArgs e)
{
    string age = textBox1.Text;
    int i = 0; // check if it is a int
    bool result = int.TryParse(age, out i) // see if it is a int
    if(result == true){ // check if it is a int
        string afe;
        string afwe2;
        afe = "You are ";
        afwe2 = " years old!";
        MessageBox.Show(afe + age + afwe2);
    } else {
        MessageBox.Show("Please Enter a Number!"); // error message
    }
}

使用正则表达式

if(!String.IsNullOrEmpty(age) && age.All(char.IsDigit))
不需要使用
+
\d
来验证人的年龄。 一个人通常活在0/113岁之间

其他方法:

使用

使用linq

if(!String.IsNullOrEmpty(age) && age.All(char.IsDigit))
如我所愿

if (int.TryParse(age, out ageAsInt) && ageAsInt <= 113)

if(int.TryParse(age,out-ageAsInt)和&ageAsInt为什么投票关闭这个太本地化的问题?这是一个非常有效的C#/Regex问题。试试这个Regex表达式\d+什么意思,你不能让它接受?如果你的意思是错误也显示在数字上,那是因为你的Regex是错误的。你可能想要类似于
@^\d+$的东西
。如果我是你,我会开始学习C#而不是正则表达式。你似乎还不太了解类型是如何工作的,你需要先了解这一点,然后才能在C#中取得任何进展。尽管正则表达式看起来很简单,但你可能会犯很多错误。此外,。这是一个处理正则表达式的绝佳工具动态。这将无法编译。您需要避开反斜杠或使用逐字字符串。@JustinMorgan:谢谢您提到这一点。我在web浏览器中进行了编辑…我已更正了示例。是的,但我没有想到:(,不过这是一个很好的建议。
if (int.TryParse(age, out ageAsInt) && ageAsInt <= 113)