February 28, 2021
Hot Topics:

Building a Regular Expression Stream Search with the .NET Framework

  • By Jeffrey Juday
  • Send Email »
  • More Articles »

IsmatchBuffer is straightforward, but MoveBuffer requires further discussion. The MoveBuffer function appears below.

private void MoveBuffer(StringBuilder builder,int byteCount)
   byte[] data;
   ASCIIEncoding encoder = new ASCIIEncoding();
   string copyValue = "";
   int actualReadCount = byteCount;

   if (builder.Length > 0)
      builder.Remove(0, byteCount);

   //Don't get more than what you actually need
   if (_stream.Length < byteCount)
      actualReadCount = (int)_stream.Length;

   //Don't get more than what is left
   if ((_stream.Position + actualReadCount) > _stream.Length)
      actualReadCount = (int)(_stream.Length - _stream.Position);

   data = new byte[actualReadCount];

   _stream.Read(data, 0, actualReadCount);

   copyValue = encoder.GetString(data);

According to the .NET documentation, StringBuilder is the recommended string copy, append, and remove class. MoveBuffer performs all of the string manipulation. As discussed earlier, you use the ASCIIEncoding class to change the Stream bytes into a string. You move the buffer along in the Stream like a sliding window along the Stream. Bytes are removed from the back of the buffer and added to the front of the buffer.


Regular Expression support is common in many development tools and applications. Although .NET supports regular expression string search via the RegEx class, it has no support for byte Streams. We developed a Stream Regular Expression search class as part of a larger effort to scan incoming email received by a POP3 configured BizTalk Receive Port.

Download the Code

You can download the code that accompanies this article here.


About the Author

Jeffrey Juday is a software developer with Crowe Chizek in South Bend, Indiana. He has been developing software with Microsoft tools for more than 12 years in a variety of industries. Jeff currently builds solutions using BizTalk 2004, ASP.NET, SharePoint, and SQL Server 2000. You can reach Jeff at jjuday@crowechizek.com.

Page 3 of 3

This article was originally published on January 4, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date