简体   繁体   中英

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]

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 ]

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