3

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

5 Answers 5

2

tis replaces all parenthesis, filled with digits by parenthesis

string str = "Test123(45)";
string result = Regex.Replace(str, @"\(\d+\)", "()");
Sign up to request clarification or add additional context in comments.

Comments

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

Try this.Use with verbatinum mode @.The lookahead will make sure number have ) without ( before it.Replace by empty string.

See demo.

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

5 Comments

Thanks alot, this works! Also great website, wasn't aware of this.
@SomeGuy08: regex101 is not handling .NET-specific regex flavor. Use regexstorm.net/tester or regexhero.net/tester
@SomeGuy08.. I would use ASh/fubo's solution.. it is simplified solution of this solution and also very performant.. (takes very less steps.. also I dont think one should use lookaheads unless there is no actual requirement)
Also, this regex will capture a number even if it is not inside parentheses: test 444). It will even replace a number if it is followed by letters before a closing round bracket: test 444string). If these cases are OK, then I have no questions. :)
This Regex is clearly wrong, according to the question. It removes all numbers that are not followed by an open parentheses. The question is about removing numbers within the parentheses.
1
string str = "Test123(45)";
string result = Regex.Replace(str, @"\(\d+\)", "()");

1 Comment

Regex.Replace(str, @"\(\d+\)", "()") will work for string in format (DigitDigitDigit); [result ()]; it will not remove Digits if string in format (DigitSomethingDigit); [result (DigitSomethingDigit)]
0

you can also try this way:

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.