March 9, 2006

Python == Perl when it comes to regex's

Filed under: Code

A friend of mine is learning python and commented how he thought it was a better programming language but perl’s regex’s were better and so was better suited to scripting. I had to set him straight so I wrote a simple script in perl and python to parse telephone numbers.

Sample data:

(206) 329-1173
206.329.1173
206-329-1173
206 329-1173
329-1173
3291173

Python:

#!/usr/bin/env python

import sys,re
phonenumber = re.compile("\(?(\d{0,3})\)?[\.\-\s]?(\d{3})[\.\-\s]?(\d{4})")
for line in sys.stdin:
   matches = re.match(phonenumber, line)
   areacode = matches.group(1)
   prefix = matches.group(2)
   extension = matches.group(3)
   if areacode == "":
       print "Prefix = " + prefix + " Extension = " + extension
   else:
       print "Area code = " + areacode + " Prefix = " + prefix
       print " Extension = " + extension

Perl:

#!/usr/bin/env perl

use warnings;
use strict;

while (<>) {
my ($areacode, $prefix, $extension) =
    ( $_ =~ /\(?(\d{0,3})\)?[\.\-\s]?(\d{3})[\.\-\s]?(\d{4})/ );
   if ($areacode eq "") {
       print "Prefix = $prefix Extension = $extension\n";
   }
   else {
       print "Area code = $areacode Prefix = $prefix "
       print "Extension = $extension\n";
   }
}

Both scripts produce the same output:

Area code = 206 Prefix = 329 Extension = 1173
Area code = 206 Prefix = 329 Extension = 1173
Area code = 206 Prefix = 329 Extension = 1173
Area code = 206 Prefix = 329 Extension = 1173
Prefix = 329 Extension = 1173
Prefix = 329 Extension = 1173

Both scripts uses the exact same regex. Python is just as powerful as Perl for regular expressions.

Leave a Reply

Powered by WordPress