简体   繁体   中英

Regex.Replace not replacing properly

I have a string (PointName) that I need to replace any of the following characters with an empty string :/?*'[]. Below is the script I have using c#:

    Regex.Replace(PointName, @"\:/?*'[]", @"")

I tried above but getting the following message:

parsing ":/?*'[]" - Unterminated [] set.

You need to create a Regex object that is associated with the pattern, then use replace.

string pattern = @"[[\]?/\\:*']";
string replacement = " ";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);

You need to use an the OR operation to create the regex statement that eliminates the characters wanted to eliminate. You can not simply just list the characters.

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