Reputation: 113
This works perfect..
result2 = Regex.Replace(result2, "[^A-Za-z0-9/.,>#:\s]", "", RegexOptions.Compiled)
But I need to allow square brackets ([
and ]
).
Does this look correct to allow Brackets without changing what is allowed and not allowed from the above?
result2 = Regex.Replace(result2, "[^A-Za-z0-9\[\]/.,>#:\s]", "", RegexOptions.Compiled)
Reason I need a second opinion is that I think if this is correct something else is blocking it that is out of my control.
Upvotes: 3
Views: 5877
Reputation: 113
I cant say any one person did or did not answer the question or try to help, I would split the solution among everyone if I could because it made me think. The key was to separate the brackets by using a single \ . Thanks everyone for your help.
result = Regex.Replace(result, "[^A-Za-z0-9/\[\].,>#\s]", "", RegexOptions.Compiled)
Upvotes: 3
Reputation: 75222
The gimmick @tripleee mentioned does indeed work in .NET. Just make sure ]
is the first character (or in this case, first after the ^
.
result2 = Regex.Replace(result2, "[^][A-Za-z0-9/.,>#:\s]", "");
But be careful about porting the regex to other flavors. Some will treat it as a syntax error, and some will treat it as two atoms: [^]
and [A-Za-z0-9/.,>#:\s]
, the first of which matches literally anything but nothing--i.e., any character including newlines.
On a side note, why are you using the RegexOptions.Compiled
option? That's something you should use only when you know you need it. The increased performance will almost never be significant, and it comes with a pretty high price tag, as explained here.
http://msdn.microsoft.com/en-us/library/8zbs0h2f.aspx
Upvotes: 0