When doing a regex search for alphabetical characters are [^A-Za-z\s] and \W\d equivalent? Is there a difference performance wise?
Asked
Active
Viewed 4,325 times
1
-
Unless your regex matching is in the middle of an extremely tight loop that is called thousands or millions of times, the execution difference between those two is so small as to be irrelevant. Therefore, choose the one that is clearer to your intent. (And no, they're not the same) – Andy Lester Jan 29 '15 at 22:39
-
1`doing a regex search for alphabetical characters` Your regex excludes them, though. – nhahtdh Jan 30 '15 at 02:33
2 Answers
4
No, they aren't equivalent. \w
is the equivalent to of A-Za-z0-9_
.
The performance would depend on the engine that you are using, but I can't imagine there being much difference between the two.

AlecBrooks
- 524
- 4
- 12
3
No, they are not the same. Try it with some test data and prove it to yourself.
\W
is the negation of \w
. \w
is "word character". It does not include spaces, punctuation, etc.
[A-Za-z\s]
is "The letters A-Z, upper and lower case, plus whitespace". [^A-Za-z\s]
is the negation of that.
Why are you asking about the difference between the two? Are you hoping to speed up a search by using one instead of the other? If so, it's very likely that your search is slow because of factors other than which of two very simple regexes you are using.

Andy Lester
- 91,102
- 13
- 100
- 152