简体   繁体   English

需要基本的正则表达式帮助

[英]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);
        }
    }

You could use this regular expression [0-9]*\\.[0-9]* . 您可以使用此正则表达式[0-9]*\\.[0-9]* The * means 0-9 can occur 0 to x times. *表示0-9可以出现0到x次。

You could use regexr to test regular expression in a fast way. 您可以使用regexr以快速方式测试正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM