1

我有如下消息,我有 2 个针对此的正则表达式。

对于正则表达式 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);
4

1 回答 1

0

这是问题所在:

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

而是替换为:

{(.*)}
于 2013-10-14T10:44:25.127 回答