Some time ago a friend introduced me to precompiled regular expressions. Now, I have always been an absolute fan of regex–it is one of the cornerstones of Perl. I have written scripts that consisted of hundreds of regexes. But since my introduction to the precompiled regex I am a full time advocate!

The beauty of a precompiled regex is that it is fast. Every time that a regular expression is evaluated it has to be compiled from its text representation into a block of byte code. This byte code is then run and it produces the regex output that we all know and love. The downside is that compiling into byte code takes time. So if you have a script that loops for a long time you will be wasting time compiling the regex over and over again. Depending on the code and the complexity of the regex this time could be significant.

In a precompiled regex you compile the regex and assign it to a scalar just once. Then when you go to use it you will simply execute the precompiled byte code–there is no need to spend time compiling it.

For example:

 

while( ––$Count )
{
    my $TimeString = GetTimeString(); 
    // Precompile and run the regex
    if( $TimeString =~ /\d{2}:\d{2} PM/i )          
    {
         print "$TimeString is in the afternoon.\n";
    }
}

 

Can be written to use a precompiled regex:

 

// Precompile the regex
my $Regex = qr/\d{2}:\d{2} PM/i; 
while( ––$Count )
{
    my $TimeString = GetTimeString(); 
    // Execute the precomiled regex
    if( $TimeString =~ $Regex )       
    {
         print "$TimeString is in the afternoon.\n";
    }
}

 

If in this code example $Count is set to 1000 then the second example is saving ( 1000 * Time_to_compile_regex ) worth of time.

There is another aspect of precompiled regexes which is really neat: you can assign different precompiled regexes to keys in a hash. Neat! emoticon