正则表达式问题 [英] Problem with Regular Expression

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

问题描述

大家好,

我正在使用常规扩展名来验证C#.Net中Windows应用程序的TextBox中的数值.
这是我的正则表达式

Hi All,

I am using a regular exression to validate numeric value in TextBox for my Windows Application in C#.Net.
This is my regular expression

private void txtNumeric_TextChanged(object sender, EventArgs e)
       {
            Regex regex = new Regex(@"^*[0-9,-]+\.?[0-9]*$");
            if (!(regex.IsMatch(txtNumeric.Text)))
            {
                txtNumeric.ResetText();
            }
        }



它工作正常,但问题是,它也接受"-2-3-2-3".
我需要我的正则表达式仅在第一个位置而不是以后接受-".
表示-"仅在开始时才是可选的.

我该如何修改我的正则表达式以满足以上条件?

谢谢和问候,
Nagendra.



It is working well but the problem is, it is also accepting "-2-3-2-3".
I need my regex to accept ''-'' only at first position and not later.
Means ''-'' should be optional only at first postion.

How should i modify my regex to fulfill above condition?

Thanks and Regards,
Nagendra.

推荐答案

); 如果(!(regex.IsMatch(txtNumeric.Text))) { txtNumeric.ResetText(); } }
"); if (!(regex.IsMatch(txtNumeric.Text))) { txtNumeric.ResetText(); } }



它工作正常,但问题是,它也接受"-2-3-2-3".
我需要我的正则表达式仅在第一个位置而不是以后接受-".
表示-"仅在开始时才是可选的.

我应该如何修改我的正则表达式以满足以上条件?

谢谢和问候,
Nagendra.



It is working well but the problem is, it is also accepting "-2-3-2-3".
I need my regex to accept ''-'' only at first position and not later.
Means ''-'' should be optional only at first postion.

How should i modify my regex to fulfill above condition?

Thanks and Regards,
Nagendra.


使用^-*[0-9]+\.?[0-9]*
Use ^-*[0-9]+\.?[0-9]*





当您改进了该问题后,我发现您正在使用JavaScript 代码进行验证.您不需要它.最好使用RegularExpressionValidator.

我使用了以下代码.您可以尝试类似的方法:




As you have improved the question, I see you are using JavaScript codes to validate. You don''t need it. Better you use a RegularExpressionValidator.

I used the following code. You may try something similar:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
      <asp:RegularExpressionValidator ControlToValidate="TextBox1" ID="RegularExpressionValidator1" ValidationExpression="^-*[0-9]+\.?[0-9]*


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

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