C# - 正则表达式使用点替换任何字符。在括号[]中

时间:2015-04-11 07:36:22

标签: c# regex replace

示例:

string str = "Example[1]";
string output = Regex.Replace(str, "[.]", "");

但它不起作用,输出仍然是:示例[1]

我的结果将是"示例"仅?

请帮助:(

2 个答案:

答案 0 :(得分:2)

你的方法是正确的..只需使用转义字符代替括号..

string output = Regex.Replace(str, @"\[.\]", "");

输出:示例

编辑:如果括号中有多个字符,请使用"\[.+?\]"

答案 1 :(得分:1)

使用以下表达式:

string output = Regex.Replace(str, @"\[\d+\]", "");

它会查找符号[,任意数量的数字和符号]