-1

I need to find strings as follows in a file:

_["Some text"];

or 

_.Plural(1, "Some text", "Some text plural);

I am looping file text lines using:

using (StreamReader reader = File.OpenText(file)) {

  String line;

  while ((line = reader.ReadLine()) != null) {

  }          
}          

In each line I need to get:

"Some text" 

OR

"Some text", "Some text plural"

And in both cases I need to get the line number inside the file for each instance.

How can I do with Regex?

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481

1 Answers1

0

Using the pattern and logic from these SO posts:
https://stackoverflow.com/a/171483/1634205
https://stackoverflow.com/a/4892517/1634205

And following this tutorial:
https://www.dotnetperls.com/regex-file

Try this:

    static void Main(string[] args)
    {
        Regex pattern = new Regex("\"(.*?)\"");

        string file = @"C:\where\your-file\is\file.txt";

        using (StreamReader reader = File.OpenText(file))
        {
            string line;

            while ((line = reader.ReadLine()) != null)
            {
                foreach (Match match in pattern.Matches(line))
                {
                    Console.WriteLine(match.Value);
                }
            }
        }

        Console.ReadLine();
    }
Ryan Taite
  • 789
  • 12
  • 37