Regular Expressions For The Win

I’m currently working on the approval application for the NJ LoCo team, and part of it is giving actual evidence of what we discussed — this includes IRC logs. Unfortunately, I never set up a LoCo bot to log the meetings, so I’m currently going through my bip logs for relevant convos. However, bip keeps very precise information. So, while in your irc client it may look like this:

16:16:22 <gQuigs> hi
16:16:49 <harda> gQuigs: You coming Saturday?
16:17:04 <gQuigs> what time does the fair end?
16:17:10 <harda> 14:00.
16:17:18 <gQuigs> oh

In my bip logs, it looks like this:

19-09-2007 16:16:22 < gQuigs!n=bryan@c-24-0-106-77.hsd1.nj.comcast.net: -hi
19-09-2007 16:16:49 < harda!n=harda@pdpc/supporter/silver/harda: +gQuigs: You coming Saturday?
19-09-2007 16:17:04 < gQuigs!n=bryan@c-24-0-106-77.hsd1.nj.comcast.net: -what time does the fair end?
19-09-2007 16:17:10 < harda!n=harda@pdpc/supporter/silver/harda: +14:00.
19-09-2007 16:17:18 < gQuigs!n=bryan@c-24-0-106-77.hsd1.nj.comcast.net: -oh

Lucky for us, we live in a world of regular expressions. A php script and some regex mojo later:

<?
   if(STDIN)
   {
      while(!feof(STDIN))
      {
         $line = trim(stream_get_line(STDIN,10240,"\n"));
         $line = preg_replace('/\d{2}-\d{2}-\d{4} (\d{2}:\d{2}:\d{2}) (?:<|>)? ([^!:]*)(?:![^:]*)?: (?:-|\+)?/','\1 <\2> ', $line); //   normal text
         $line = preg_replace('/\d{2}-\d{2}-\d{4} (\d{2}:\d{2}:\d{2}) (?:<|>)? ([^!:]*)(?:![^ ]*)? /','\1 \2 ', $line); //emotes
         $line = preg_replace('/\d{2}-\d{2}-\d{4} (\d{2}:\d{2}:\d{2}) (?:-!-)? ([^!:]*)(?:![^ ]*)? /','\1 \2 ', $line); // actions
         echo $line."\n";
      }
   }
?>

And we’re right as rain (results can be seen here). That can probably be condensed some, and you could, of course, do that in whatever language you like. On a side note, you won’t realize how difficult WordPress’s Rich Text Editor can really be, until you have to wrestle it into showing properly indented code. Every time I save this post, WordPress mangles my code again.


JoeTerranova.net Print This Post Print This Post