Reputation: 21
When doing a regex search for alphabetical characters are [^A-Za-z\s] and \W\d equivalent? Is there a difference performance wise?
Upvotes: 1
Views: 5424
Reputation: 1336
Even with the correction of \W vs \w:
\w and [A-Za-z] are not equivalent.
There are (surprise!) other languagues than English, that have word characters outside the range of a-z. \w includes them, a-z does not.
Upvotes: 1
Reputation: 520
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.
Upvotes: 4
Reputation: 93676
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.
Upvotes: 3