Skip to content

RegexFindReplaceStream

jamesfoster edited this page Feb 2, 2012 · 1 revision

The RegexFindReplaceStream can be used to find occurances of a regular expression and replace them with the result of an evaluator method.

var replacements = new Dictionary<Regex, Func<Match, string>>
  {
    {new Regex("_([^_\n\r]+)_"), m => string.Format("<b>{0}</b>", m.Groups[1].Value)},
    {new Regex("abc"), m => "xyz"}
  }
var maxMatchLength = 1024;

var output = new FileStream("output.txt", FileMode.OpenOrCreate, FileAccess.Write)
var input = new FileStream("input.txt", FileMode.Open, FileAccess.Read)

using(output)
using(var replaceStream = input.RegexFindReplace(replacements, maxMatchLength, Encoding.UTF8)
{
  replaceStream.CopyTo(output);
}

In this example we scan the input for occurances of 2 regular expressions. When it finds an occurance it calls the matching lambda expression passing in the Match.

Note: the maxMatchLength is specified as 1024 bytes. Any matching occurance under this length will successfully be captured. You may, however, still receive longer occurances (up to maxMatchLength * 2).

Limitations:

  • Matches can not overlap.

    For example if the input contained _abc_ the first regex would match and return <b>abc</b> and scanning would continue from the end of that match. In order to get the output of <b>xyz</b> you would need to wrap one RegexFindReplaceStream inside another.

  • The order of the patterns matters.

    If 2 patterns match at the same position, the first in the list of replacements succeeds.

Clone this wiki locally