简体   繁体   English

正则表达式替换不正确

[英]Regex.Replace not replacing properly

I have a string (PointName) that I need to replace any of the following characters with an empty string :/?*'[]. 我有一个字符串(PointName),我需要用空字符串:/?*'[]替换以下任意字符。 Below is the script I have using c#: 以下是我使用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. 您需要创建与模式关联的Regex对象,然后使用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. 您需要使用OR操作来创建正则表达式语句,以消除要消除的字符。 You can not simply just list the characters. 您不能只列出字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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