Chris McGrath
Chris McGrath

Reputation: 1757

regex not removing square brackets

i am using the following regex [^a-zA-Z\d!-] pattern in c# to clean special characters from filename so i can pull basic information from it and build initial class property values from it

how ever no matter what i seem to do it wont clean the square brackets from the string according to regex builder it should be matching the square brackets but is not removing them when i run the replace operation

any help as to why would be greatly appreciated and a fix would be nice too =)

the code im using in c# is as follows:

var removeSpecChar = new Regex(@"[^a-zA-Z\d!-]");               

msa = msa.Substring(0, msa.Length - 4);
removeSpecChar.Replace(msa, " ").Trim();

Upvotes: 1

Views: 1035

Answers (1)

Lucero
Lucero

Reputation: 60190

Didn't try, but you really should escape the - in characher sets:

var removeSpecChar = new Regex(@"[^a-zA-Z\d!\-]"); 

Also note that \d is not equivalent to 0-9, it matches any unicode digit (including arabic digits etc.). So you may want to change that if this isn't your intention.

Upvotes: 1

Related Questions