My friend I never knew I needed – Mastering Regular Expressions

Although I have made money as a programmer for quite a few years, it
took me a while to actually introduce myself to people as a
“programmer”. Partly because I didn’t like Dungeons and Dragons, bathed
regularly and couldn’t (didn’t want to) do simple calculations in my
head.

These days, I go by the title of “student”, else I prefer to tell
people I’m a “project manager”, “IT consultant” or something vaguely to
do with IT. When I graduate university, I’d like to continue on with
getting IT project management certified and stray as far away from the
title of “programmer” as possible. Not that I don’t like programmers,
some of my best workers friends are great coders.

Recently though, I have been talking with some friends who are also
getting on in years and discovered I don’t really have any hobbies right
now. I used to ride motorbikes in Japan, go to the gym in China, but
here in Hong Kong, as a busy student, I don’t often want to go out
anywhere far…. So, I have decided to make PROGRAMMING my new hobby!

Get to the point!

While I used to do some pretty fancy stuff with different programming
languages I was always scared to dive into regular expressions. Every
now and then I would slightly modify an expression I found online or
have one of my friends or workers write something for me, but it always
looked like a foreign language.

Now, as a City University student, I have access to an awesome
physical and online library of tech books. One such book I grabbed on
first site, was this O’Reilly one on regular expressions:

Mastering Regular Expressions

I’m still only a couple of chapters in, but my fears of regular
expression complexity have been alleviated and I’m now able to find more
creative and efficient ways to solve new and existing problems using
any programming language required.

Here is a solution I’ve whipped up this morning as my first Perl and
regular expression script, which solves a problem of incorrect mail
headers. I had previously used OS X Mail to “redirect” 160,000 emails
from a local server into Gmail, using AppleScript to circumvent some
flakiness in other methods available to migrate lots of email into
Gmail/Google Apps Mail. The issue with using Mail’s redirect function to
do this, is that it adds a bunch of stuff to the headers such as
“Resent-to”, Resent-from”, as well as changing the mail’s date in the
inbox to show it’s redirection date, not the original date. In most
cases, this is the way you would want it to work, but not for this bulk
migration I had performed.

Along comes my new friend, Regex, to the rescue!

Just run this script from the top level directory of all the emails needing modifying and it does it’s thing:

#!/usr/bin/perl

################################
# #
# Batch email meta modifier #
# #
################################

#declare variables
$dateToInsert = “”;
$firstCompilationOfOutput = “”;

# run this script from the top level directory, as follows:

# for i in `find . -type f -name ‘*emlx’`; do perl -w /path/to/the/script/rewriteScript $i > tempFile.tmp; mv tempFile.tmp $i; done;

while ($line = <>)
{
#remove any Resent and Return Path… lines completely
$line =~ s/^Resent(.)*n//g;
$line =~ s/^Return-Path(.)*n//g;
$line =~ s/^(.)* (PDT)n//g;

if ($line =~ m/^Date: (.*)n/i)
{
#store the correct date from the original email
$dateToInsert = $1;
}

$firstCompilationOfOutput .= $line;
}

#replace first instance in whole variable which can match below, with the original email’s date…

#………..
#Received: from somedomain.com ([72.10.34.54])
# by mx.google.com with ESMTPS id dt11si677614vcb.69.2011.09.13.21.09.02
# (version=TLSv1/SSLv3 cipher=OTHER);
# Fri, 3 Sep 2010 21:09:03 -0700 (PDT)
#Authentication-Results: sdgdgfdgdgssfg
#……..etc

$firstCompilationOfOutput =~ s/(?<=Received:)(.)*(?=nAuthentication-Results)/ $dateToInsert/s;

#print (to file) each processed line of data
foreach $line (split /n/, $firstCompilationOfOutput)
{
print $line.”n”;
}

#stage left
exit;

Share on twitter
Twitter
Share on reddit
Reddit
Share on telegram
Telegram
Share on email
Email
Share on print
Print

Site made with love of these open source tools

The beautifully-crafted OS that forces you to learn how to do things properly.

For better or worse, WordPress powers 30% of the web. ClassicPress reduces some of the bloat.

Text is my material. Learn one text editor well.

Version controlling all the things.

Retaining workspaces on local and remote servers.

Supporting the OpenBSD community with opinionated VMs. €10/yr donated to the OpenBSD Foundation.

To Roman Zolotarev, for helping us Master the Web. My family and friends for enduring my voluntary financial hardship while pursuing my passions.