2013-11-05 70 views
0

我已經發布了這個,因爲我以前的帖子被公認是相當含糊不清。在一個字符串中的大括號內捕獲一個數字c#

我有一個字符串,我想捕獲它裏面的數字,然後添加一個它!

比如我有一個電子郵件主題標題說:「回覆:你好(1)」

我要拍攝1,然後由2提高它,然後3,然後4,等等。我遇到的困難是考慮到越來越多的數字,一旦它變成10或100,那麼額外的數字會殺死我當前的正則表達式。

任何幫助將一如既往地受到讚揚!

 int replyno; 
    string Subject = "Re: Hey :) (1)"; 
    if (Subject.Contains("Re:")) 
    { 
     try 
     { 
      replyno = int.Parse(Regex.Match(Subject, @"\(\d+\)").Value); 
      replyno++; 
      Subject = Subject.Remove(Subject.Length - 3); 
      TextBoxSubject.Text = Subject + "("+replyno+")"; 
     } 
     catch 
     { 
      TextBoxSubject.Text = Subject + " (1)"; 
     } 

    } 
    else 
    { 
     TextBoxSubject.Text = "Re: " + Subject; 
    } 

從這個代碼電流輸出從Int.TryParse

回答

0

問題出在您刪除並替換回復編號的方式。

這樣更改代碼

int replyno; 
    string Subject = "Re: Hey :) (1)"; 
    if (Subject.Contains("Re:")) 
    { 
     try 
     { 
      replyno = int.Parse(Regex.Match(Subject, @"(\d+)").Value); 
      replyno++; 
      Subject = Regex.Replace(Subject,@"(\d+)", replyno.ToString()); 
      TextBoxSubject.Text = Subject ; 
     } 
     catch 
     { 
      TextBoxSubject.Text = Subject + " (1)"; 
     } 

    } 
    else 
    { 
     TextBoxSubject.Text = "Re: " + Subject; 
    } 
+0

美麗的隊友,感謝您抽出寶貴的時間給我提供一個確切的解決方案。 – Pearce

0

失敗,我通常不應對正則表達式所以這裏的如何我會做到這一點。

string subject = "Hello (1)"; 
string newSubject = string.Empty; 

for (int j = 0; j < subject.Length; j++) 
    if (char.IsNumber(subject[j])) 
     newSubject += subject[j]; 

int number = 0; 

int.TryParse(newSubject, out number); 
subject = subject.Replace(number.ToString(), (++number).ToString()); 
0

你不一定需要爲這個正則表達式,但你可以調整你到\((?<number>\d+)\)$來解決這個問題。

對於一個正則表達式的解決方案,您可以用一組訪問比賽:

for (int i = 0; i < 10; i++) 
{ 
    int currentLevel = 0; 
    var regex = new System.Text.RegularExpressions.Regex(@"\((?<number>\d+)\)$"); 

    var m = regex.Match(inputText); 
    string strLeft = inputText + " (", strRight = ")"; 
    if (m.Success) 
    { 
    var levelText = m.Groups["number"]; 
    if (int.TryParse(levelText.Value, out currentLevel)) 
    { 
     var numCap = levelText.Captures[0]; 

     strLeft = inputText.Substring(0, numCap.Index); 
     strRight = inputText.Substring(numCap.Index + numCap.Length); 
    } 
    } 

    inputText = strLeft + (++currentLevel).ToString() + strRight; 

    output.AppendLine(inputText); 
} 

相反,只考慮使用的IndexOf和字符串:

// Example 
var inputText = "Subject Line"; 

for (int i = 0; i < 10; i++) 
{ 
    int currentLevel = 0; 
    int trimStart = inputText.Length; 

    // find the current level from the string 
    { 
    int parenStart = 0; 
    if (inputText.EndsWith(")") 
     && (parenStart = inputText.LastIndexOf('(')) > 0) 
    { 
     int numStrLen = inputText.Length - parenStart - 2; 
     if (numStrLen > 0) 
     { 
     var numberText = inputText.Substring(parenStart + 1, numStrLen); 
     if (int.TryParse(numberText, out currentLevel)) 
     { 
      // we found a number, remove it 
      trimStart = parenStart; 
     } 
     } 
    } 
    } 

    // add new number 
    { 
    // remove existing 
    inputText = inputText.Substring(0, trimStart); 

    // increment and add new 
    inputText = string.Format("{0} ({1})", inputText, ++currentLevel); 
    } 

    Console.WriteLine(inputText); 
} 

主要生產

Subject Line 
Subject Line (1) 
Subject Line (2) 
Subject Line (3) 
Subject Line (4) 
Subject Line (5) 
Subject Line (6) 
Subject Line (7) 
Subject Line (8) 
Subject Line (9) 
Subject Line (10) 
0

嘗試用此代碼代替:

var m = Regex.Match(Subject, @"\((\d+)\)"); 
replyno = int.Parse(m.Groups[1].Value); 

的變化是:

  • 捕捉只是在正則表達式中位數
  • 解析剛剛捕獲的數字

我也建議您檢查m.Success而不是僅僅捕獲由此產生的異常。

相關問題