Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

regex

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.

like image 386
TomatoMato Avatar asked Aug 15 '13 00:08

TomatoMato


People also ask

How do I match a character except space in regex?

You can match a space character with just the space character; [^ ] matches anything but a space character.

Which pattern is used to match any non What character?

The expression \w will match any word character. Word characters include alphanumeric characters ( - , - and - ) and underscores (_). \W matches any non-word character.

What does \\ mean in regex?

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).

What is a non digit character?

Non-digit characters are any characters that are not in the following set [0, 1, 2, 3, 4 ,5 ,6 ,7 ,8, 9] .


1 Answers

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&]
like image 138
Rohit Jain Avatar answered Sep 22 '22 01:09

Rohit Jain