简体   繁体   English

C#-Regex使用点替换任何字符。 在括号[]中

[英]C# - Regex replace any character using dot . in bracket [ ]

Example: 例:

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

But it doesnt work, the output is still: Example[1] 但是它不起作用,输出仍然是:Example [1]

I though the result will be "Example" only? 我虽然结果仅是“示例”?

Please help :( 请帮忙 :(

Your approach is correct.. just use escape characters for brackets.. 您的方法是正确的..只需在括号中使用转义符即可

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

Output : Example 输出 :示例

EDIT : if you have more than one characters in brackets.. use "\\[.+?\\]" 编辑 :如果您在方括号中使用多个字符,请使用"\\[.+?\\]"

Use the following expression: 使用以下表达式:

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

It looks for symbol [ , any number of digits and symbol ] 它查找符号[ ,任意数字和符号]

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

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