How can I use Regex for TextReader?

What is the best way to search for a pattern in (potentially) very large text.

I could use Regex, but it takes a string as an argument. Is there a way to use it with a TextReader or some kind of thread?

+3
source share
2 answers

No, regex may need to be rolled back. Since the stream is only being read ahead, this will mean that it should still support the entire stream in memory. Even if you have a regular expression that will not return, the engine is not designed for this.

, . , .

+2

( ), . , . (, , "".::))

var pattern = new Regex(@"\b\w+\b");

using (var reader = new StreamReader(@"..\..\TextFile1.txt"))
{
    while (reader.Peek() >= 0)
    {
        Match match = pattern.Match(reader.ReadLine());
        while (match.Success)
        {
            Console.WriteLine(match.Value);
            match = match.NextMatch();
        }
    }
}

-, , . . , , ReadLine() , . .

+2

Source: https://habr.com/ru/post/1730441/


All Articles