TomatoMato
TomatoMato

Reputation: 713

RegEx: match any non-word and non-digit character except

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.

Upvotes: 8

Views: 15681

Answers (1)

Rohit Jain
Rohit Jain

Reputation: 213223

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&]

Upvotes: 12

Related Questions