arya
arya

Reputation: 235

Regex to find special characters in a String with some exceptions

I just had a similar (but not exact) question answered. Now I need help with the question mentioned below.

I want to write a regex which matches a character if its a non word, non digit and non star (*) character. So, the characters [0-9][a-z][A-Z] * should not match and the others should.

I tried writing [\W[^*]] but it doesn't seem to work.

Upvotes: 8

Views: 63972

Answers (4)

Jaime Arias
Jaime Arias

Reputation: 69

Please try the following regex:

[\W_]

Upvotes: 0

user65952
user65952

Reputation: 202

[^\w\*]

Simple enough.

Upvotes: 7

Tom Alsberg
Tom Alsberg

Reputation: 7033

The simplest regular expression that matches a single character, that is not one of those you described, independent of any particular regular-expression extensions, would be:

[^0-9a-zA-Z *]

Upvotes: 7

Zach Scrivena
Zach Scrivena

Reputation: 29569

Try this instead:

[^\w\*]

Upvotes: 20

Related Questions