0

我已经再次发布了这个,因为我之前的帖子被承认是相当模棱两可的......对不起!

我有一个字符串,我想捕获其中的数字,然后添加一个!

例如,我有一个电子邮件主题标题,上面写着“Re: Hello (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 失败

4

4 回答 4

1

尝试替换此代码:

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

这些变化是:

  • 仅捕获正则表达式中的数字
  • 仅解析捕获的数字

我还建议您检查 m.Success 而不是仅仅捕获生成的异常。

于 2013-11-05T01:37:38.227 回答
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());
于 2013-11-05T01:34:42.937 回答
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 和 Substring:

// 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) 
于 2013-11-05T01:34:51.133 回答
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;
     }
于 2013-11-05T01:56:34.657 回答