by taco_emoji · 11y ago · 0 comments
I'd rather word this as "more specific is better". Like say for a U.S. phone number (minus area code for simplicity),
\d{3}-?\d{4}
is better than .*-?.*
because it's more specific."Longer is better" is only useful for helping identify which regex is better, not for helping write better regexes.
"In most flavors that support Unicode, \d includes all digits from all scripts." [1]
EDIT: But I guess for the majority of use cases it doesn't matter [2] since PCRE is the norm almost everywhere.
[1] http://www.regular-expressions.info/shorthand.html
[2] "Notable exceptions are Java, JavaScript, and PCRE. These Unicode flavors match only ASCII digits with \d"
[0-9]{3}-?[0-9]{4}
is even more specific (and faster) because \d would match other digit characters outside of [0-9].[0-9][0-9][0-9]-?[0-9][0-9][0-9][0-9]