Remove numbers in a specific part of a string (in parentheses)
I have a string Test123 (45) and I want to remove the numbers in the parenthesis. How should I do it?
So far I have tried the following:
string str = "Test123(45)";
string result = Regex.Replace(str, "(\\d)", string.Empty);
However, this causes Test () to fail when it should be Test123 () .
source to share
Remove the number actually inside the parentheses , but not the parentheses, and don't keep anything else inside them that is not a number since C # Regex.Replace
means matching all bracketed substrings with \([^()]+\)
and then removing all the numbers inside MatchEvaluator
.
Here is a Sample C # 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 enclosed in characters (
and )
, ASh \(\d+\)
solution will work well: \(
matches literal (
, \d+
matches 1+ digits, \)
matches literal )
.
source to share