简单的正则表达式验证中的错误 [英] error in simple regex validation

查看:78
本文介绍了简单的正则表达式验证中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个确定时间的文本框,在表单加载时我已经编写了代码,以便时间将填充在这种格式的文本框中

09:09 AM

i我正在使用正则表达式进行验证

所以我有这个代码

  private   bool  time()
{
Regex regex = new 正则表达式( ^(1 [0-2] | 0 [1-9]):[0 -5] [0-9] \040(AM | AM | PM |时)$);
if (regex.IsMatch(textBox2.Text))
{
return true ;
}
其他
{
返回 ;
}



}



表格中有一个按钮,用于显示错误是有错误提供程序显示错误,如果没有,那么错误提供程序不会闪烁,按钮单击的代码是



问题是当我保持文本框空白并单击按钮,错误提供程序不显示错误

即使我把''''09:09'''''而不是''''09:09 pm'' ''它没有显示错误

解决方案

);
if ( regex.IsMatch(textBox2.Text))
{
return true ;
}
其他
{
返回 false ;
}



}



表格中有一个按钮,用于显示错误是否存在错误提供程序显示错误,如果没有,则错误提供程序不会闪烁,按钮单击的代码是



问题是我保持文本框空白并单击按钮,错误提供者没有显示错误

即使我把''''09:09'''''而不是'''''09:09'''''它没有显示错误


请参阅我对该问题的评论。我发现了一个错误:你忘了逃避'':''。让它''\:'',逃脱。



此外,我会大大简化它,在Regex验证后通过C#进行额外验证。这样做:



使用模式 ^([0-9] [0-9])\ :( [0-9] [0-9])(AM | PM)


,并进行不区分大小写的匹配。



它将在圆括号内为您提供三个字符串字符串组。用数字提取两个组并测试每个组以与数字进行比较:

  string  digitGroupText =  //   ...取自Regex  
// 它将是00到99之间的任何值,但只有00到59才有效......

byte number = byte .Parse(digitGroupText); // 当传递正则表达式验证时,这应该始终成功

< span class =code-keyword> if (number > 59 // ...然后它无效





有这个主意吗?正则表达式并不适合每一次验证。



祝你好运,

-SA

i have a textbox which is determining the time, on the form load i have written the code so that the time will be filled in the textbox which is in this format
09:09 AM
i am using the regex for validation purpose
so i have wirtten this code

private bool time()
        {
            Regex regex = new Regex("^(1[0-2]|0[1-9]):[0-5][0-9]\040(AM|am|PM|pm)$");
            if (regex.IsMatch(textBox2.Text))
            {
                return true;
            }
            else
            {
                return false;
            }

       
       
        }


there is a button in the form which is for showing if the error is there then the error provider shows the error, and if not, then error provider doesnt blink, the code for button click is

the problem is when i keep the textbox blank and clicks the button, the error provider doesnt shows the error
even if i put ''''09:09 st'''' instead of ''''09:09 pm'''' it doesnt shows error

解决方案

"); if (regex.IsMatch(textBox2.Text)) { return true; } else { return false; } }


there is a button in the form which is for showing if the error is there then the error provider shows the error, and if not, then error provider doesnt blink, the code for button click is

the problem is when i keep the textbox blank and clicks the button, the error provider doesnt shows the error
even if i put ''''09:09 st'''' instead of ''''09:09 pm'''' it doesnt shows error


Please see my comment to the question. I found a bug: you forgot to escape '':''. Make it ''\:'', escaped.

Besides, I would greatly simplified it, employing additional validation via C# after Regex validation. Do this:

Use the pattern ^([0-9][0-9])\:([0-9][0-9]) (AM|PM)


, and make the case-insensitive match.

It will give you three string string groups, inside round brackets. Extract two groups with digits and test each for comparison with numbers:

string digitGroupText = //... take from Regex
// it will be anything from 00 to 99, but only 00 to 59 is valid...

byte number = byte.Parse(digitGroupText); // as Regex validation is passed, this should always be successful

if (number > 59) //... then it is invalid



Got the idea? Regex along is not suitable for each and every validation.

Good luck,

—SA


这篇关于简单的正则表达式验证中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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