-1

I am making a parser that parses .cs files. After the .cs file is uploaded on the website i have to parse everything that is inside the namespace of the .cs file. I want to acccomplish this with regular expressions but i have no idea how to do this since i am new to regular expressions.

How would i write a expression that outputs everything inside the first and last bracket of the .cs file? For example, this is a simple .cs file:

namespace Application
{ <----------------------------------
    class Members
    {
       int testInt = 10;
       string testString = 'test';

       public void testMethod()
       {
       }
    }
} <----------------------------------

I want to get all the code between the arrows in my code example.

I have heard and read some stuff about groups but i dont really understand it yet. I would appreciate if someone could help me.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
stefan de boer
  • 389
  • 1
  • 8
  • 20
  • You are going in the wrong direction trying to use Regexes to parse .cs files. Yes, you will be able to create an expression to parse this specific snippet, but you will discover each snippet will require a different expression. If you want to parse a programming language you will have to [build a compiler](https://stackoverflow.com/questions/1669/). – Dour High Arch Jun 19 '20 at 07:40
  • @DourHighArch Thank you! I know that that is probably the best way to build a parser i just dont have alot of time to read into it. Since i only have a few days to get this done i thought regular expressions would be the faster option. Also, since i am not a C# expert, building a compiler by myself is probably out of my league for now. – stefan de boer Jun 19 '20 at 07:53
  • Using Regexes will not be faster because you will have to create an infinite number of them and you will not live that long. If you're **only** interested in extracting code in namespaces you can just build the scanner portion. But then you say “everything inside the first and last bracket” and that is not the same thing as a namespace. It would really help if you [explained the problem you are trying to solve, not just what solution you have decided to use](https://meta.stackexchange.com/questions/66377/). – Dour High Arch Jun 19 '20 at 08:13

1 Answers1

3

You can get the value between the brackets without regular expression.

try
{
    string output = input.Substring(input.IndexOf('{') + 1, input.LastIndexOf('}') - 1);
}
catch (Exception ex)
{
  //Code to handle this          
}

Note: you need to handle the case, where string can be null and if it does not have ending {.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • Thank you! However your code throws this error ``System.ArgumentOutOfRangeException: 'Index and length must refer to a location within the string. '`` – stefan de boer Jun 19 '20 at 07:49
  • I have tested, what input are you trying to pass? – Vivek Nuna Jun 19 '20 at 07:54
  • I am trying to pass a string with all the code of the .cs file in it like my example above. Could it be the ``using System`` etc that is causing the problem? That is the only difference between the code above and the code i try to pass. – stefan de boer Jun 19 '20 at 08:09
  • @Stefan be aware this only handles the specific situation in your question; exactly one namespace with no code, comments, strings, attributes, or anything with braces outside that one namespace. It does not handle general .cs files. – Dour High Arch Jun 19 '20 at 16:44