-1

I am trying to use an OpenFileDialog to read all text / lines from a file(it doesn't matter what kind of file), and extract only C# Keywords(I already have the keywords typed into a string) -But, I can't seem to figure out how to extract ALL occurrences. I don't want to count them, like count++, I want to display the occurrences in a RichTextBox

This is the code that only gets the first occurrence:

        string keywords = @"\b(default|delegate|do|else|event|explicit|extern|false|finally|fixed|for|foreach|goto|if|implicit|in|interface|internal|is|lock|namespace|new|null|object|operator|out|override|params|private|protected|public|readonly|ref|return|sealed|sizeof|stackalloc|switch|this|throw|true|try|typeof|unchecked|unsafe|using|virtual|volatile|while)\b";         
        MatchCollection matches = Regex.Matches(File.ReadAllText(ofdd.FileName), keywords);              
        foreach (Match match in matches)
        {
            richTextBox1.Text = (match.Groups[1].Value);
        }

-I know I forgot keywords like abstract, etc.

That code only performs part of the job :( I need it to display ALL occurrences of the keywords string

Any idea how to display ALL occurrences?

Momoro
  • 599
  • 4
  • 23
  • this should help you: https://stackoverflow.com/questions/2425847/current-line-and-column-numbers-in-a-richtextbox-in-a-winforms-application – Kevin Oct 27 '19 at 06:00
  • can you share the output desire? – Umair Anwaar Nov 01 '19 at 06:33
  • My output desire: If a file is written like: "interface, house, public", then I want it to only display `public` and `interface`, not `house` - I want it to display all occurrences of C# keywords – Momoro Nov 01 '19 at 07:03

1 Answers1

0

I finally created this code from many different answers on different sites, and it works perfectly:

               string[] keys = { "abstract", "as", "base", "break", "case", "catch", "checked", "continue", "default", "delegate", "do", "else", "event", "explicit", "extern", "false", "finally", "fixed", "for", "foreach", "goto", "if", "implicit", "in", "interface", "internal", "is", "lock", "namespace", "new", "null", "object", "operator", "out", "override", "params", "private", "protected", "public", "readonly", "ref", "return", "sealed", "sizeof", "stackalloc", "switch", "this", "throw", "true", "try", "typeof", "unchecked", "unsafe", "using", "virtual", "volatile", "while" };

                    char[] separators = new char[] { ',', ' ', '\r', '\n', '.' };

                    String[] strings = data.Split(separators);
                    StringBuilder stringBuilder = new StringBuilder();
                    foreach (var item in strings)
                    {
                        if (keys.Contains(item))
                        {
                            stringBuilder.Append(item + " ");
                            count = item.Length;
                        }
                    }
                    string theOutputDesire = stringBuilder.ToString();

data would be the input, e.g. if a file or a string contained public, people, and empty, then only public would be displayed.

count is the amount of occurrences in a file, e.g. if a file contained public, public, and people then count would be 2.

keys contains the text we are looking for. It can be modified to your needs.

I hope this helps anyone else looking for an answer!

Momoro
  • 599
  • 4
  • 23