简体   繁体   中英

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 = 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.

Update

I've implemented TryParse but am looking to accept decimal numbers with or without 0 in front, ie 0.234 or .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]* . The * means 0-9 can occur 0 to x times.

You could use regexr to test regular expression in a fast way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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