How can I use Regex against a TextReader?

.net, regex

Solution

No, a regular expression may need to do backtracking. As a stream only is read forward it would mean that it had to keep the entire stream in memory anyway. Even if you have a regular expression that wouldn't backtrack, the engine isn't built for this.

Besides, regular expressions isn't very fast anyway. You should look for a pattern matching method that is designed for reading streams.

Problem

What's the best way to look for a pattern in a (potentially) very large text. I could use Regex but it accepts a string as an argument. Is there a way to use it with a TextReader or some kind of stream instead?

Original source