0

I have the following code in a text file:

static const char* g_FffectNames[EFFECT_COUNT] = {
"Fade In and Fade Out",
"Halloween Eyes",
"Rainbow Cycles"
};

I can use g_FffectNames[EFFECT_COUNT] as a starting point to search in this big text file. But I need to get the things within quotes (e.g Halloween Eyes or Rainbow Cycles).

What is the best way to get those text in C#? I would also have to assume that there are more code on top of this file (before the static const) and also toward the bottom (after the }; ) and that spacing between characters such as = { or }; is optional to the user.

Should I compress all of these lines into one string and start the search or should I use some sort of regex matching to make this easier?

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
JonL
  • 53
  • 5
  • 1
    This isn't C# code. That looks like C++. –  Jul 24 '17 at 16:25
  • 3
    @Amy It looks like OP is writing a parser in C# that reads C declarations. – Sergey Kalinichenko Jul 24 '17 at 16:26
  • I think the C++ is in the text file, although why one would need to parse such a file escapes me. – Andy G Jul 24 '17 at 16:26
  • Have you tried something? The first and last sentence of your description indicate that you have an idea of how to start - why not write a bit of code and update your question when you get stuck. – kaveman Jul 24 '17 at 16:34
  • Amy, the code in the text file is in C++, But I need to find text between those lines in C# and then append new items in quotes afterward. – JonL Jul 24 '17 at 16:37

2 Answers2

0

You can use a regular expressions to parse input file:

var input = /* file content */;
var regex = new Regex("^\\s*\"(?<row>[^\"]+)\\s*\"", RegexOptions.Multiline);
var values = regex.Matches(input)
    .Cast<Match>()
    .Select(m => m.Groups["row"]).ToArray();
Aleks Andreev
  • 7,016
  • 8
  • 29
  • 37
0

I got it working based on this post:

c# search string in txt file

public string readText()
        {
           string test = string.Empty;
           var mytext = File.ReadLines("C:\\temp\\test_search.txt")
                .SkipWhile(line => !line.Contains("g_FffectNames[EFFECT_COUNT]"))
                .Skip(1)
                .TakeWhile(line => !line.Contains("};"));

            foreach (var line in mytext)
            {
                test += line;
            }

            return test;
        }
JonL
  • 53
  • 5