I have a string Test123(45) and I want to remove the numbers within the parenthesis. How would I go about doing that?
So far I have tried the following:
string str = "Test123(45)";
string result = Regex.Replace(str, "(\\d)", string.Empty);
This however leads to the result Test() , when it should be Test123() .
tis replaces all parenthesis, filled with digits by parenthesis
string str = "Test123(45)";
string result = Regex.Replace(str, @"\(\d+\)", "()");
\d+(?=[^(]*\))
Try this.Use with verbatinum
mode @
.The lookahead will make sure number have )
without (
before it.Replace by empty string
.
See demo.
string str = "Test123(45)";
string result = Regex.Replace(str, @"\(\d+\)", "()");
you can also try this way:
string str = "Test123(45)";
string[] delimiters ={@"("};;
string[] split = str.Split(delimiters, StringSplitOptions.None);
var b=split[0]+"()";
Remove a number that is in fact inside parentheses BUT not the parentheses and keep anything else inside them that is not a number with C# Regex.Replace
means matching all parenthetical substrings with \\([^()]+\\)
and then removing all digits inside the MatchEvaluator
.
Here is a C# sample program :
var str = "Test123(45) and More (5 numbers inside parentheses 123)";
var result = Regex.Replace(str, @"\([^()]+\)", m => Regex.Replace(m.Value, @"\d+", string.Empty));
// => Test123() and More ( numbers inside parentheses )
To remove digits that are enclosed in (
and )
symbols, the ASh's \\(\\d+\\)
solution will work well: \\(
matches a literal (
, \\d+
matches 1+ digits, \\)
matches a literal )
.
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.