与雷吉斯搏斗一下。
我想要实现的是接受字符串和交换两位数,如果它们被除以"-“,而第一位大于第二位。
示例:
1,3,5,14-10, 15-20
应改为:
1,3,5,10-14,15-20
非常感谢您的帮助,因为到目前为止,我只有字符串验证器:
"^(?!([ \\d]*-){2})\\d+(?: *[-,] *\\d+)*$"不确定在这里实现我提出的问题的做法是什么(我的意思是--修改现有的问题或在后处理时使其生效)。
发布于 2016-09-14 02:17:24
您可以使用带有Regex.Replace实例的MatchEvaluator方法作为第三个参数来调用返回替换字符串的函数。示例:
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string sContent = "1,3,5,14-10,15-20";
MatchEvaluator evaluator = new MatchEvaluator(LowerThan);
Console.WriteLine(Regex.Replace(sContent, @"(\d+)-(\d+)", evaluator));
}
public static string LowerThan(Match match)
{
if (int.Parse(match.Groups[1].Value) > int.Parse(match.Groups[2].Value)) {
return match.Groups[2].Value + "-" + match.Groups[1].Value;
}
return match.Value;
}
}https://stackoverflow.com/questions/39480978
复制相似问题