Shortening your regular expression match in perl

Normally, when you pattern match using a regular expression in Perl, your expression will match as much as possible.

For example,

$match = "hello to you world what in the world were you thinking";

$match =~ /hello.*world/;

$match is now "hello to you world what in the world"

If you want to make this match the least amount and still satisfy your regular expression, you’d do the following:

$match =~ /hello.*?world/;

$match is now "hello to you world"

Placing the ? after your wildcard expression basically tells it to expand as little as possible before matching the following pattern.