{3} in regex mean exactly 3 times but

Haviv Elbsz 2,071 Reputation points
2023-11-20T08:16:10.4166667+00:00

hello. I use this pattern in my c# application (\d+){3} and I get matches like 245 , 5678 , 123456 , 678 . . . when I expect to get exactly {3} 3 numbers with digits size of 1 or greater. please can someone explain what is my wrong. thank you very much

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,819 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 44,231 Reputation points Microsoft Vendor
    2023-11-21T07:34:26.4233333+00:00

    Hi @Haviv Elbsz , Welcome to Microsoft Q&A,

    I think you should use it in conjunction with code judgment.

    Don't when the number in the obtained group is greater than 3.

    make judgments

    using System;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    using System.Windows.Forms;
    
    namespace xxx
    {
         public partial class Form1 : Form
         {
             public Form1()
             {
                 InitializeComponent();
             }
    
             private void Form1_Load(object sender, EventArgs e)
             {
                 textBox1.Text = "6";
                 textBox2.Text = "34sad 464dff";
                 textBox3.Text = "34sad 464dff asdas 45647ff";
                 textBox4.Text = "34sad 464dff 546asdas 45647ff";
             }
    
             //Use the regular \d+( \d+){0,2} to determine that the content in the textbox is returned as a list. If the elements of the list are more than 3 or equal to 0, false is returned.
             static List<int> ExtractNumbers(string input)
             {
                 List<int> result = new List<int>();
    
                 //Define regular expression
                 Regex regex = new Regex(@"\d+( \d+)?( \d+)?");
    
                 // Match string
                 MatchCollection matches = regex.Matches(input);
    
                 //Extract matching numbers
                 foreach (Match match in matches)
                 {
                     foreach (Capture capture in match.Captures)
                     {
                         int number;
                         if (int.TryParse(capture.Value, out number))
                         {
                             result.Add(number);
                         }
                     }
                 }
    
                 return result;
             }
    
             static bool isValid(List<int> numbers)
             {
                 if (numbers.Count == 0 || numbers.Count > 3)
                 {
                     return false;
                 }
                 else
                 {
                     return true;
                 }
             }
             private void button1_Click(object sender, EventArgs e)
             {
                 foreach (Control c in this.Controls)
                 {
                     if (c is TextBox)
                     {
                         List<int> numbers = ExtractNumbers(c.Text);
    
                         if (isValid(numbers))
                         {
                             c.Text += "Valid";
                         }
                         else
                         {
                             c.Text += "inValid";
                         }
                     }
                 }
             }
         }
    }
    
    

    User's image

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Viorel 116.6K Reputation points
    2023-11-20T08:38:44.48+00:00

    I think that you should use (\d{3})+.

    A number like 5678 is matched by (\d+){3} because \d+ can be repeated three times: \d\d, \d, \d.

    Or use ^(\d{3})+$ depending on context.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.