我已经发布了这个,因为我以前的帖子确实相当模糊。不错!!
我有一个字符串,我想捕获其中的数字,然后添加一个!。
例如,我有一个电子邮件主题标题“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
而失败答案 0 :(得分:1)
尝试替换此代码:
var m = Regex.Match(Subject, @"\((\d+)\)");
replyno = int.Parse(m.Groups[1].Value);
变化是:
我还建议您检查m.Success而不是仅仅捕获生成的异常。
答案 1 :(得分: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());
答案 2 :(得分: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)
答案 3 :(得分: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;
}