正则表达式:获取每个在其自身之前或之后没有其他数字的数字
Regex: get every number that doesn't has an other number before or after itself
我必须做的是提取每个 'single' 号码,它本身之前或之后没有其他号码。然后我必须用一个数字替换这个数字,该数字以 1 递增。
我现在想要一个从字符串中获取每个数字的正则表达式,它在其自身之前或之后没有其他数字。
例如:
From this string, I just want the '1': "Klasse 1a 14/15" the result should look like "Klasse 2a 14/15"
From this string, I just want the '5': "5/66" the result should look like "6/66"
From this string, I want the '1', '2' and '3': "1/2/3" and the result should look like "2/3/4"
From this string, I just want the '4': "4. Klasse" and the result should look like "5. Klasse"
From this string, I don't want to get anything: "Klasse 99" and the result will remain "Klasse 99"
我目前拥有的是这个正则表达式:
[\D*](\d{1})[\D*]
和这个方法:
internal string GenerateFollowingSubjectGradeTitle(string currentGradeTitle)
{
var followingGradeTitle = string.Format("{0}{1}{0}", "_", currentGradeTitle);
var regex = new Regex(@"[\D*](\d){1}[\D*] /gm");
var matches = regex.Matches(followingGradeTitle);
if (matches.Count > 0)
{
for (var i = 0; i < matches.Count; i++)
{
var exactmatch = matches[i].Groups[1].Value;
var groupmatch = matches[i].Groups[0].Value;
var intmatch = 0;
if (int.TryParse(exactmatch, out intmatch))
{
var groupreplace = groupmatch.Replace(exactmatch, (intmatch + 1).ToString());
followingGradeTitle = followingGradeTitle.Replace(groupmatch, groupreplace);
}
}
}
return followingGradeTitle.Substring(1, followingGradeTitle.Length - 2);
}
..它大部分时间都有效,但不是每次都有效。在“1/2/3”中,我只得到了 1 和 3,但我还需要 2。
我知道我的尝试不是最好的方法,如果你有更好的解决方案,请告诉我:)
有什么建议吗?
我必须做的是提取每个 'single' 号码,它本身之前或之后没有其他号码。然后我必须用一个数字替换这个数字,该数字以 1 递增。
我现在想要一个从字符串中获取每个数字的正则表达式,它在其自身之前或之后没有其他数字。
例如:
From this string, I just want the '1': "Klasse 1a 14/15" the result should look like "Klasse 2a 14/15"
From this string, I just want the '5': "5/66" the result should look like "6/66"
From this string, I want the '1', '2' and '3': "1/2/3" and the result should look like "2/3/4"
From this string, I just want the '4': "4. Klasse" and the result should look like "5. Klasse"
From this string, I don't want to get anything: "Klasse 99" and the result will remain "Klasse 99"
我目前拥有的是这个正则表达式:
[\D*](\d{1})[\D*]
和这个方法:
internal string GenerateFollowingSubjectGradeTitle(string currentGradeTitle)
{
var followingGradeTitle = string.Format("{0}{1}{0}", "_", currentGradeTitle);
var regex = new Regex(@"[\D*](\d){1}[\D*] /gm");
var matches = regex.Matches(followingGradeTitle);
if (matches.Count > 0)
{
for (var i = 0; i < matches.Count; i++)
{
var exactmatch = matches[i].Groups[1].Value;
var groupmatch = matches[i].Groups[0].Value;
var intmatch = 0;
if (int.TryParse(exactmatch, out intmatch))
{
var groupreplace = groupmatch.Replace(exactmatch, (intmatch + 1).ToString());
followingGradeTitle = followingGradeTitle.Replace(groupmatch, groupreplace);
}
}
}
return followingGradeTitle.Substring(1, followingGradeTitle.Length - 2);
}
..它大部分时间都有效,但不是每次都有效。在“1/2/3”中,我只得到了 1 和 3,但我还需要 2。
我知道我的尝试不是最好的方法,如果你有更好的解决方案,请告诉我:)
有什么建议吗?