Building a Regular Expression Stream Search with the .NET Framework
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
- http://en.wikipedia.org/wiki/Regular_expression
- .NET Framework documentation
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.
More for Developers
On the Codeguru Forums
Visit the Forums »Featured Partner Resources
Get your Android Apps ready for Intel® Atom™ processor-based smartphones and tablets now.
Use the Android NDK to deliver the best performance on Intel® Atom™ processor-based devices.
The Android community on the Intel® Software Network has everything you need to prepare your apps for Intel® Atom™ processor-based devices.



Solid state disks (SSDs) made a splash in consumer technology, and now the technology has its eyes on the enterprise storage market. Download this eBook to see what SSDs can do for your infrastructure and review the pros and cons of this potentially game-changing storage technology.