Reputation: 1339
I have this code that replaces all non alphanumeric characters with "-" char.
return Regex.Replace(strIn, @"[\W|_]+", "-", RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase);
but I need to change it to allow pass some special characters (one or more) for example: #,*,%
how to change this regular expression?
Upvotes: 3
Views: 3083
Reputation: 91373
How about this one:
[^a-zA-Z0-9#*%]+
If you are using unicode you can do (as Tim's answer):
[^\p{L}\p{N}#*%]+
Upvotes: 2
Reputation: 75222
You can use set subtraction for that:
@"[\W_-[#*%]]+"
This matches the set of all non-word characters and the underscore, minus the set of #
, *
and %
.
Note that you don't have to use |
for "or" in a character class, since that's implied. In fact, the |
in your regex just matches |
.
Note also that in .NET, \w
matches a few other "connector punctuation" characters besides the underscore. If you want to match the other characters too, you can use
@"[\W\p{Pc}-[#*%]]+"
Upvotes: 0
Reputation: 336098
Use
[^\p{L}\p{N}#*%]+
This matches one or more characters that are neither letters nor digits nor any of #
, *
or %
.
Upvotes: 4
Reputation: 136074
Another option, you can use charcter class subtractioninfo, for example to remove #
from the character class:
[\W_-[#]]+
Just add other accepted special chars after the #
. Live example here: http://rextester.com/rundotnet?code=YFQ40277
Upvotes: 2
Reputation: 1790
Use this.
([^\w#*%]|_)
Add any other special characters after the %.
It is basically saying, match any character that is not (^) a word character(\w), #, * or % OR match _.
Upvotes: 1