tags:

views:

123

answers:

4

I want to exclude the following characters from my string:

\--
'
<
>

Please tell me how to write a regular expression for this.

A: 

If your regex dialect supports lookaheads:

^(?:(?!\\--|['<>]).)*$

However, in some languages it might be cleaner to have a simple manual check rather than use a regex.

Max Shawabkeh
The range `--'` is invalid. At least in .NET's regex implementation.
Joey
Right you are. I just mechanically copied the characters, not even noticing there were two dashes. With your edit to the OP, it seems the whole sequence is to be considered.
Max Shawabkeh
A: 
string s = Regex.Replace(SomeString, "[\-'<>]", "");

Hope this helps.

NawaMan
what's that `` there?
Joey
Sorry ... I was in hurry.
NawaMan
+2  A: 

Personally I'd just use string.Replace. Regular expressions are great, but should be used wisely.

juharr
A: 

If the question is how to remove \-- and ' and < and > from a string then this regex does the job:

['<>]|\\--

or in C#

resultString = Regex.Replace(subjectString, @"['<>]|\\--", "");
Jan Goyvaerts