4

I know there are many questions about making regular expressions, but they all seem to be about a single problem than the general usage. I, too, have a problem like to solve. I have tried to learn by reading about regular expressions, but it gets tricky quick. Here's my question:

C#

I need to validate two textboxes that exist on the same form. The math operations I've coded can handle any floating point number. For this particular application I know of three formats the numbers will be in or there is a mistake on the users behalf. I'd like to prevent those mistakes in example if an extra number is accidentally typed or if enter is hit too early, etc.

Here are the formats: "#.####" "##.####" "###.##" where the "#" represents a mandatory digit. The formats starting with a one or two digit whole number must have 4 trailing digits or more. I've capped it at 8, or so I tried to lol.The format starting with a three digit whole number should never be allowed to have more than two digits trailing the decimal.

Here's what I have tried thus far.

    Regex acceptedInputRegex = new Regex(@"^\b[0-9]{3}.[0-9]{2}|[0-9]{1,2}.[0-9]{4,8}$");

    Regex acceptedInputRegex = new Regex(@"^\b\d{3}.\d{2} | \d{1,2}.\d{4,8}$");

I have tried it in thinking a match was what I wanted to achieve and as if a match to my negated expression means there is a problem. I was unsuccessful in both attempts. This is the code:

    if (acceptedInputRegex.IsMatch(txtMyTextBox1.Text) || acceptedInputRegex.IsMatch(txtMyTextBox2.Text))
            {

            } else
            {
                MessageBox.Show("Numbers are not in the right format", "Invalid Input!");
                return;
            }
  1. Are regular expressions what I should be using to solve this problem?
  2. If not, please tell me what you recommend. If so, please help me correct my regex.

Thanks.

DavidG
  • 785
  • 8
  • 23
  • 3
    In the future you can use this site to test your expressions: [regex101.com](https://regex101.com/). I find it very handy and use it whenever I have to build them, it beats testing them at run time in your code. – Igor Jul 27 '17 at 10:54
  • 1
    Well, mind that http://regex101.com does not support .NET syntax. Use Ultrapico Expresso (free tool, no affiliation on my part) that provides a similar functionality for .NET regex flavor. – Wiktor Stribiżew Jul 27 '17 at 10:58
  • Thanks for the info. Will look into it now. – DavidG Jul 27 '17 at 11:01

1 Answers1

4

You are close, you need to escape the dots and group the alternatives so that the ^ and $ anchors could be applied to both of them:

@"^(?:\d{3}\.\d{2}|\d{1,2}\.\d{4,8})$"

See the regex demo.

Details:

  • ^ - start of string
  • (?: - start of a non-capturing group matching either of the two alternatives:
    • \d{3}\.\d{2} - 3 digits, . and 2 digits
    • | - or
    • \d{1,2}\.\d{4,8} - 1 or 2 digits, ., 4 to 8 digits
  • ) - end of the non-capturing group
  • $ - end of string.

To make \d match only ASCII digits, use RegexOptions.ECMAScript option:

var isValid = Regex.IsMatch(s, @"^(?:\d{3}\.\d{2}|\d{1,2}\.\d{4,8})$", RegexOptions.ECMAScript);
Graham
  • 7,431
  • 18
  • 59
  • 84
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    I am glad I was even close. Today, trying to learn this was like trying to learn Arabic. It looked very foreign to me. Your answer was very detailed. It worked and I was able to understand your description. Thanks. – DavidG Jul 27 '17 at 11:12