none
Why are groups in Regex doesn't works correctly RRS feed

  • Question

  • Match match = Regex.Match(textBox1.Text, @"(\d)*(\d)");
                MessageBox.Show(match.Groups[1].Value.ToString());
    double x2 =Convert.ToDouble(match.Groups[1].Value.ToString());
     double x1 = Convert.ToInt32(match.Groups[2].Value.ToString());
    
    textBox1.Text = Regex.Replace(textBox1.Text, @"\d*\d", (x1 * x2).ToString());

    I have been trying write a math parser and I catch a problem with regex groups for example

    textBox.Text = 2*2

    and match.Groups[1].Value is empty.

    Monday, May 27, 2019 7:06 PM

Answers

  • @Jack, please take a closer look at your solution. It cannot work. 

    For once, you missed the special meaning of '*' as regular expression. Therefore, you need to escape it to get hold of the multiplication operator, just as Viorel_ does in his Regex proposal.

    And then, the line 

    double x2 = Convert.ToDouble(match.Groups[0].Value.ToString());
    would throw an exception, if you were using a correct Regex pattern, because match.Groups[0].Value, the first element of a GroupCollection, returns always a string that matches the entire regular expression pattern (i.e. the string "2*2" that could not be converted to double). The reason that it does work anyway is your faulty Regex pattern. Your pattern is testing for zero or more numbers, followed by another number. So, match.Groups[0] will catch just the "2" and skips the rest of the input string because it cannot catch the "*" . The OP's code was correct regarding the handling of the Groups collection. He should try Viorel_'s Regex pattern.
    • Marked as answer by Sewwe Tuesday, May 28, 2019 1:59 PM
    Tuesday, May 28, 2019 9:58 AM

All replies

  • Hi Sewwe,

    Thank you for posting here.

    For your question, you want to write a math parser by using regex.

    You could try the following code, the bold code is modified.

      private void Button1_Click(object sender, EventArgs e)
            {
                Match match = Regex.Match(textBox1.Text, @"(\d)*(\d)");
                MessageBox.Show(match.Groups[0].Value.ToString());
                double x2 = Convert.ToDouble(match.Groups[0].Value.ToString());
                double x1 = Convert.ToInt32(match.Groups[2].Value.ToString());
    
                textBox1.Text = Regex.Replace(textBox1.Text, @"\d*\d", (x1 * x2).ToString());
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 28, 2019 5:20 AM
    Moderator
  • Try this pattern:

     

       (\d+)\s*\*\s*(\d+)


    • Edited by Viorel_MVP Tuesday, May 28, 2019 6:47 AM
    • Proposed as answer by Wizend Tuesday, May 28, 2019 9:59 AM
    Tuesday, May 28, 2019 5:32 AM
  • @Jack, please take a closer look at your solution. It cannot work. 

    For once, you missed the special meaning of '*' as regular expression. Therefore, you need to escape it to get hold of the multiplication operator, just as Viorel_ does in his Regex proposal.

    And then, the line 

    double x2 = Convert.ToDouble(match.Groups[0].Value.ToString());
    would throw an exception, if you were using a correct Regex pattern, because match.Groups[0].Value, the first element of a GroupCollection, returns always a string that matches the entire regular expression pattern (i.e. the string "2*2" that could not be converted to double). The reason that it does work anyway is your faulty Regex pattern. Your pattern is testing for zero or more numbers, followed by another number. So, match.Groups[0] will catch just the "2" and skips the rest of the input string because it cannot catch the "*" . The OP's code was correct regarding the handling of the Groups collection. He should try Viorel_'s Regex pattern.
    • Marked as answer by Sewwe Tuesday, May 28, 2019 1:59 PM
    Tuesday, May 28, 2019 9:58 AM