I'm just going to sidestep the issue of readability for now.
First lets look at what each version compiles down to.
perl -Mre=debug -e'/^\d{4}$/'
Compiling REx "^\d{4}$"
synthetic stclass "ANYOF[0-9][{unicode_all}]".
Final program:
1: BOL (2)
2: CURLY {4,4} (5)
4: DIGIT (0)
5: EOL (6)
6: END (0)
anchored ""$ at 4 stclass ANYOF[0-9][{unicode_all}] anchored(BOL) minlen 4
Freeing REx: "^\d{4}$"
perl -Mre=debug -e'/^\d\d\d\d$/'
Compiling REx "^\d\d\d\d$"
Final program:
1: BOL (2)
2: DIGIT (3)
3: DIGIT (4)
4: DIGIT (5)
5: DIGIT (6)
6: EOL (7)
7: END (0)
anchored ""$ at 4 stclass DIGIT anchored(BOL) minlen 4
Freeing REx: "^\d\d\d\d$"
Now I'm going to see how well each version performs.
#! /usr/bin/env perl
use Benchmark qw':all';
cmpthese( -10, {
'loop' => sub{ 1234 =~ /^\d{4}$/ },
'repeat' => sub{ 1234 =~ /^\d\d\d\d$/ }
});
Rate loop repeat
loop 890004/s -- -10%
repeat 983825/s 11% --
While the /^\d\d\d\d$/
does consistently run faster, it isn't significantly faster. Which really just leaves it down to readability.
Let's take this example to the extreme:
/^\d{32}$/;
/^\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d$/;
I don't think there are many people who would argue that the second example is easier to read.
If we take it to the other extreme, the first style seems downright redundant.
/^\d{1}$/;
/^\d$/;
So what it really comes down to, is how many repetitions of \d
, before your preference switches from just repeating the \d
, to using a quantifier.