2013-10-14 79 views
1

我有如下消息,我有2個正則表達式針對此。C#正則表達式匹配

對於正則表達式rh,它運行良好。雖然對於正則表達式rh1,我無法弄清楚,匹配應該返回類似"{{0:0.00}%,{ABND,1000},/,{OFRD,1002}}"而不是"{0:0.00"這將被最終值替換。

我希望有任何建議。

T.Format = "T1 Message: {{0:0.00}%,{ABND,1000},/,{OFRD,1002}}"; 


Regex rh = new Regex(@"{{(.*?)}(.*?),{(.*?),(.*?)},(.*?),{(.*?),(.*?)}}"); 
Match mh = rh.Match(T.Format); 
Regex rh1 = new Regex(@"{(.*?)}"); 
Match mh1 = rh1.Match(T.Format); 
decimal decReturn = 0; 
string h1 = mh.Groups[1].Value.ToString(); 
string h2 = mh.Groups[2].Value.ToString(); 
string h3 = mh.Groups[3].Value.ToString(); 
string h4 = mh.Groups[4].Value.ToString(); 
string h5 = mh.Groups[5].Value.ToString(); 
string h6 = mh.Groups[6].Value.ToString(); 
string h7 = mh.Groups[7].Value.ToString(); 

switch (h5) 
{ 
    case "+": decReturn = 0; break; 
    case "-": decReturn = 0; break; 
    case "*": decReturn = 0; break; 
    case "/": decReturn = Convert.toInt32(h7) > 0 ? Convert.toInt32(h4)/Convert.toInt32(h7) * 100 : 0 * 100; 
          break; 
    default: throw new Exception("invalid logic"); 
} 

string strReplace = mh1.Groups[1].Value.ToString(); 
string strReturn = string.Empty; 
strReturn = Convert.ToString(decReturn); 
strReturn = Convert.ToString(T.Format).Replace(strReplace, strReturn); 
+0

你必須改進你的問題。代碼沒有說明你想要達到什麼。你能否向我們提供預期的投入和預期的投入? – mortb

+0

反正;這是作業嗎? – mortb

回答

0

這裏的問題是:

{(.*?)} 
    ^
    This makes a lazy match, one character at time 

與相反取代:

{(.*)}