简体   繁体   中英

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.

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

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.

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

希望这可以帮助。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM