To match any non-word and non-digit character (special characters) I use this: [\\W\\D]
. What should I add if I want to also ignore some concrete characters? Let's say, underscore.
You can match a space character with just the space character; [^ ] matches anything but a space character.
The expression \w will match any word character. Word characters include alphanumeric characters ( - , - and - ) and underscores (_). \W matches any non-word character.
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).
Non-digit characters are any characters that are not in the following set [0, 1, 2, 3, 4 ,5 ,6 ,7 ,8, 9] .
First of all, you must know that \W
is equivalent to [^a-zA-Z0-9_]
. So, you can change your current regex to:
[\\W]
This will automatically take care of \D
.
Now, if you want to ignore some other character, say &
(underscore is already exluded in \W
), you can use negated character class:
[^\\w&]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With