h1

Base64 Regular Expression

February 14th, 2007

Here’s another thing I thought should have been easily accesible from a simple Google Search, but alas. This regular expression was created in C# .NET 2.0 to extract Base64 text from an InfoPath XML file so that my indexing algorithm wouldn’t waste its time on it. It will recognize Base64 strings 20 characters or greater (this is necessary, otherwise it will match good ‘ole english words as Base64). Simply change the 20 to a lower or higher number if you need to.

Oh yeah, here’s a cool link on working with Regular Expressions in .NET.

The Regular Expression
[0-9a-zA-Z\+/=]{20,}

The Code Snippet
string rawString = "";

FileInfo fInfo = new FileInfo("../../TextFile1.txt");
using (StreamReader sReader = new StreamReader(fInfo.Open(FileMode.Open)))
rawString = sReader.ReadToEnd();

Regex regExBase64 = new Regex(@"[0-9a-zA-Z\+/=]{20,}");
rawString = regExBase64.Replace( rawString, "" );

2 comments to “Base64 Regular Expression”

  1. Hello !!!! :)
    I am Piter Kokoniz. oOnly want to tell, that your blog is really cool
    And want to ask you: is this blog your hobby?
    Sorry for my bad english:)
    Thank you!
    Your Piter Kokoniz, from Latvia


  2. Yeah, you could say it’s my hobby. I post stuff like this when I figure something out that I think took too long to figure out in hopes that someone in the same situation will find this solution quickly.


Leave a Comment