Regex

I’m still an absolute n00b when it comes to regex. Even after reading Mastering Regular Expressions. But man, is it powerful.


Tonight my wife asked me to help her create name badge labels for an org she’s in. All she had was an email that had a list of names like this:


DI- PAID IN FULL
Lynn Melton 2
Judy Pake 3
EO-PAID IN FULL
Peggy Ponto 9
ES – PAID IN FULL
Janice Norman 2
Janice Benson 3


Where the first two characters of the lines in upper case indicate the chapter the people on the following lines belong to. Each name line has the name followed by the table # they are to sit at.


I could have manually massaged this into a CSV file that I could do a mail merge with, but I figured it would take me about an hour to do. What a great excuse to actually try to write some regular expression code:

open(INFILE,  “source.txt”);
$chapter = “none”;
$line = “”;
while ($line = <>)
{
if ($line =~ /^(.*)[*-].*(?=PAID)/)
{
$chapter = $1;
}
if ($line =~ /^(\D*) *(\d+)/)
{
print $1;
print “, “;
print $2;
print “, “;
print $chapter;
print “\n”;
}
}

I cheated and didn’t bother trying to figure out how to concatinate strings when calling print in Perl, didn’t remove whitespace (can someone point out what I should have done?), etc… But this only took me about 15 minutes using the aforementioned book and ActivePerl.

1 comment


  1. Ll says:

    I think you can do:

       print $1.“, “.$2.“, “.$chapter.“n”;Or   print “$1, $2, $chaptern”;

    BTW I Hate Perl. I’ve read somwhere that “Perl is the only language that seems the same before and after RSA-encryption”. And I agree!And I bet there are languages easier for you to begin learning regular expressions!

Debate this topic with me:

This site uses Akismet to reduce spam. Learn how your comment data is processed.