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 () .

+3


source to share


5 answers


\d+(?=[^(]*\))

      

Try this.Use with verbatinum

mode @

. As a result, make sure the number has )

without (

before it. Replace with empty string

.



See demo.

https://regex101.com/r/uE3cC4/4

+1


source


tis replaces all parentheses filled with numbers with parentheses



string str = "Test123(45)";
string result = Regex.Replace(str, @"\(\d+\)", "()");

      

+2


source


string str = "Test123(45)";
string result = Regex.Replace(str, @"\(\d+\)", "()");

      

+1


source


you can also try:

 string str = "Test123(45)";
        string[] delimiters ={@"("};;
        string[] split = str.Split(delimiters, StringSplitOptions.None);
        var b=split[0]+"()";

      

0


source


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 )

.

0


source







All Articles