October 20, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Building a Regular Expression Stream Search with the .NET Framework

  • January 4, 2008
  • 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);
   builder.Append(copyValue);
}

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.

Conclusion

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.

Sources

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



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel