2

In shell scripting we have \t for tab , \s for whitespace , \w for word. What are \W (capital W) and \D (capital D) used for ?

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
XYZ_Linux
  • 2,527
  • 5
  • 23
  • 32

2 Answers2

8

\W is the opposite of \w and \D is the opposite of \d.

It's just like \S is the opposite of \s.

\W and \D respectively will match what \w and \d respectively don't match.

You can have a look at this site for some more explanation.

\w typically matches [A-Za-z0-9_] (ignoring the foreign characters)

\W thus matches [^A-Za-z0-9_]

And since

\d typically matches [0-9] (ignoring the foreign digits)

\D thus matches [^0-9]

Jerry
  • 67,172
  • 12
  • 92
  • 128
  • 1
    This answer has been added to the [Stack Overflow Regular Expression FAQ](http://stackoverflow.com/a/22944075/2736496), under "Character Classes". – aliteralmind Apr 10 '14 at 00:19
6

According to the manual:

\W  Match a non-word character
\D  Match a non-digit character

\W matches any character that is not matched by \w. Likewise \D matches any character that is not matched by \d.

konsolebox
  • 60,986
  • 9
  • 83
  • 94