Any tips to improve this code?

TheUninvited

New Coder
Jul 6, 2019
8
1
3
So basically i've been trying to convert sublime snippets to visual studio. So far i created this to grab all the snippets inside the sublime text and i am wondering if there's any way to improve the way of finding the elements in the for loop? My code works you know but to me i am trying to make this converter for training and learning purposes i know there is a snippet designer out there i can probably just do everything with my hands and i can still be fine it would take less time than trying to copy and paste my snippets from sublime. But still i just thought why not do this :p Here is the code and video :

The code:
Code:
 public void regexReplaceSublimeParameters(int num, List<string> mylist)
    {     
          
      Regex maxReg = new Regex(@"(\${)" + num + @"(.*?)(?=})");
      Match maxMatch = maxReg.Match(subInText);

 for (int i = 0; i < ReturnCounter(); i++)
      {
        if (maxMatch.Success)
        {
          maxReg = new Regex(@"(\${)" + num + @"(.*?)(?=})");
          maxMatch = maxReg.Match(subInText);
          mylist.Add(maxMatch.Value);
          num++;
        }           
      }

 foreach (string list in mylist)
      {
        Console.WriteLine(list);
      }
  }


    public int ReturnCounter()
    {
      
      Regex maxReg = new Regex(@"(\${)" + counter + @"(.*?)(?=})");
      Match maxMatch = maxReg.Match(subInText);

      while (maxMatch.Success)
      {
      
        if (!maxMatch.Success)
        {
          break;
        }
        else
        {
          counter++;
          maxReg = new Regex(@"(\${)" + counter + @"(.*?)(?=})");
          maxMatch = maxReg.Match(subInText);
        }
        
      }

      
      return counter;

    }
The video:
 

eeflores

New Coder
Jun 26, 2019
17
7
3
Sorry if I wasn't clear in my previous suggestion, but it's possible to create a regular expression that will capture all the elements from the input text and give you back a collection. The Regex Class documentation on the Microsoft website (https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex?view=netframework-4.8) gives an example of how to do this, using the Regex.Matches() function. The regex I suggested in your other post will give you that collection. The advantage of this approach is that you won't need to create a specific regex with unique integer to match every integer value in the input text.
 

Malcolm

Administrator
Administrator
Jan 2, 2018
828
204
55
Canada
codeforum.org
I think you could start with comments e.g. how does the code work, and what does it do.

Documentation is key, it helps you and others remember what does what.
 
  • Like
Reactions: TheUninvited