Akshay
Akshay

Reputation: 151

How to write a regular expression for excluding some special characters from string?

I want to exclude the following characters from my string:

\--
'
<
>

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

Upvotes: 0

Views: 3382

Answers (4)

Jan Goyvaerts
Jan Goyvaerts

Reputation: 21999

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, @"['<>]|\\--", "");

Upvotes: 0

NawaMan
NawaMan

Reputation: 25687

string s = Regex.Replace(SomeString, "[\-'<>]", "");

Hope this helps.

Upvotes: 0

juharr
juharr

Reputation: 32266

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

Upvotes: 2

Max Shawabkeh
Max Shawabkeh

Reputation: 38603

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.

Upvotes: 0

Related Questions