簡體   English   中英

刪除字符串特定部分的數字(在括號內)

[英]Remove numbers in specific part of string (within parentheses)

我有一個字符串Test123(45) ,我想刪除括號內的數字。 我該怎么做呢?

到目前為止,我嘗試了以下內容:

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

然而,當它應該是Test123()時,這導致結果Test ()

tis替換所有括號,用括號括起來的數字

string str = "Test123(45)";
string result = Regex.Replace(str, @"\(\d+\)", "()");
\d+(?=[^(]*\))

嘗試this.Use與verbatinum模式@ .The先行將確保數量有)(由it.Replace之前empty string

見演示。

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

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

你也可以這樣試試:

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

刪除實際上括號內的數字但不是括號並在其中保留其他任何不是C# Regex.Replace的數字Regex.Replace表示所有括號子串與\\([^()]+\\) 匹配 ,然后刪除所有數字在MatchEvaluator

這是一個C#示例程序

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 )

要刪除()符號中包含的數字, ASh的\\(\\d+\\)解決方案將運行良好: \\(匹配文字(\\d+匹配1+位, \\)匹配文字)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM