[英]Basic Regex help needed
I'm really new to programming and haven't come across Regex until now, I am looking to restrict textbox input to double class values only and came across this: 我真的很喜欢编程,到目前为止还没有遇到Regex,我希望将文本框输入限制为只有双类值,并且遇到了这个:
`Regex regex = new Regex("[^0-9-]+");
TextP1_TextChanged = regex.IsMatch(TextP1.Text);`
I want to implement into my program and am assuming it occurs under the TextChanged
event, but I don't actually have the knowledge to implement regex and so am just looking for any help. 我想实现我的程序,并假设它发生在TextChanged
事件下,但我实际上没有实现正则表达式的知识,所以我只是寻求任何帮助。
Update 更新
I've implemented TryParse
but am looking to accept decimal numbers with or without 0
in front, ie 0.234
or .234
. 我已经实现了TryParse
但我希望接受前面带或不带0
十进制数,即0.234
或.234
。 My new code is this: 我的新代码是这样的:
private void TextP1_TextChanged(object sender, EventArgs e)
{
bool isDouble = Double.TryParse(TextP1.Text, out P1);
if(isDouble == false)
{
MessageBox.Show("Text box only accepts positive number values", "Text entered into P1 is invalid", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.