I was performing a code review for a colleague and he had a regular expression that looked like this:
if ($value =~ /^\d\d\d\d$/) {
#do stuff
}
I told him he should change it to:
if ($value =~ /^\d{4}$/) {
#do stuff
}
To which he replied that he preferred the first for readability (I find the second more readable, but that’s a religious debate I’ll save for another day).
My question: is there an actual benefit to one over the other?
They do the exact same thing, so as far as practicality it’s a matter of preference. Is there a tiny performance difference one way or the other? Who knows but it’s surely insignificant.
The quantifiers are more useful (and required) when the pattern length isn’t fixed, for example
\d{12,16},\d{2,}, etc.I prefer
\d{4}which is easier for my brain to parse than\d\d\d\dAlso what if you’re matching a character class rather than a simple digit?
[aeiouy0-9]{4}or[aeiouy0-9][aeiouy0-9][aeiouy0-9][aeiouy0-9]?