Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

12. Regular Expressions > Unconstrained Repetitions

Unconstrained Repetitions

Be specific when matching “as much as possible”.

The .* construct is a particularly blunt and ponderous weapon, especially under /s. For example, consider the following parser for some very simple language, in which source code, data, and configuration information are separated by % and & characters (which are otherwise illegal):

# Format is: <statements> % <data> & <config>...

if ($source =~ m/\A  (.*)  %  (.*)  &  (.*) /xms) {
    my ($statements, $data, $config) = ($1, $2, $3);

    my $prog = compile($statements, {config=>$config});
    my $res  = execute($prog, {data=>$data, config=>$config});
}
else {
    croak 'Invalid program';
}

Under /s, the first .* will successfully match the entire string in $source. Then it will attempt to match a %, and immediately fail (because there’s none of the string left to match). At that point the regex engine will backtrack one character from the end of the string and try to match a % again, which will probably also fail. So it will backtrack one more character, try again, backtrack once more, try again, et cetera, et cetera, et cetera.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint