Character classes are special notations that matches any character from a certain set. They are also called as Metacharacters. The commonly used Character classes are:
.
(dot character) for any character except \n (a newline). Use 's' flag if you want to include it.\d
for digits.\D
for non digits.\s
for spaces, tabs, newlines.\S
for non spaces.\w
for alphanumeric and _ (underscore).\W
for all but \w.\w
matches the word (alphanumerical) characters. This pattern is same as [0-9a-zA-Z_]
.
\W
matches non-word (non-alphanumerical) characters. This pattern is same as [^0-9a-zA-Z_]
. The \W
pattern matches new-line character as well as seen below.
Lenovo Z6 Pro, releasing on 23rd April, has Snapdragon 855, 12GB of RAM, 4 cameras and a 4045 mAh battery. It'll cost $800.85 after a 15% discount.
match | position |
---|---|
Lenovo | 0 |
Z6 | 7 |
Pro | 10 |
releasing | 15 |
on | 25 |
23rd | 28 |
April | 33 |
has | 40 |
Snapdragon | 44 |
855 | 55 |
12GB | 60 |
of | 65 |
RAM | 68 |
4 | 73 |
cameras | 75 |
and | 83 |
a | 87 |
4045 | 89 |
mAh | 94 |
battery | 98 |
It | 107 |
ll | 110 |
cost | 113 |
800 | 119 |
85 | 123 |
after | 126 |
a | 132 |
15 | 134 |
discount | 138 |
Lenovo Z6 Pro, releasing on 23rd April, has Snapdragon 855, 12GB of RAM, 4 cameras and a 4045 mAh battery. It'll cost $800.85 after a 15% discount.
match | position |
---|---|
6 | |
9 | |
, | 13 |
24 | |
27 | |
32 | |
, | 38 |
43 | |
54 | |
, | 58 |
64 | |
67 | |
, | 71 |
74 | |
82 | |
86 | |
88 | |
93 | |
97 | |
. | 105 |
' | 109 |
112 | |
$ | 117 |
. | 122 |
125 | |
131 | |
133 | |
% | 136 |
. | 146 |
\d
matches digits. This pattern is same as [0-9]
.
\D
matches non-digits. This pattern is same as [^0-9]
.
\s
matches space characters and \S
is negation of it. Try and see.
Lenovo Z6 Pro, releasing on 23rd April, has Snapdragon 855, 12GB of RAM, 4 cameras and a 4045 mAh battery. It'll cost $800.85 after a 15% discount.
match | position |
---|---|
6 | 8 |
23 | 28 |
855 | 55 |
12 | 60 |
4 | 73 |
4045 | 89 |
800 | 119 |
85 | 123 |
15 | 134 |
Lenovo Z6 Pro, releasing on 23rd April, has Snapdragon 855, 12GB of RAM, 4 cameras and a 4045 mAh battery. It'll cost $800.85 after a 15% discount.
match | position |
---|---|
Lenovo Z | 0 |
Pro, releasing on | 9 |
rd April, has Snapdragon | 30 |
, | 58 |
GB of RAM, | 62 |
cameras and a | 74 |
mAh battery. It'll cost $ | 93 |
. | 122 |
after a | 125 |
% discount. | 136 |