Match.index不能正常工作

时间:2016-02-25 10:27:31

标签: c# regex

我试图在文字中找到[Key]字词并添加内容。

class Program
{
    private string x = "public class Table \n{\n[Key]\r\n[MaxLength(36)]\r\npublic string Char366{ get; set; }\npublic short? Blooean{ get; set; }\n[Key]\r\npublic bool Bit1{ get; set; }\npublic byte? Tinyinttunsigned{ get; set; }\npublic byte[] Binaryy{ get; set; }\npublic byte[] Varbinaryy{ get; set; }\npublic byte[] Blobb{ get; set; }\npublic byte[] Longblobb{ get; set; }\npublic DateTime Datetimee{ get; set; }\npublic decimal Decimall{ get; set; }\npublic double Doublee{ get; set; }\npublic short? Smallintt{ get; set; }\npublic int? Intt{ get; set; }\npublic long Bigintt{ get; set; }\npublic short Tinyintt{ get; set; }\n[Key]\r\npublic float Floatt{ get; set; }\n[MaxLength(1)]\r\npublic string Charr{ get; set; }\n[MaxLength(100)]\r\npublic string Varcharr{ get; set; }\npublic string Textt{ get; set; }\npublic string Longtextt{ get; set; }\npublic TimeSpan Timee{ get; set; }\npublic int? Mediumintt{ get; set; }\npublic long Biggintt{ get; set; }\npublic DateTime Datee{ get; set; }\npublic DateTime Timestampp{ get; set; }\npublic short Yearr{ get; set; }\npublic byte[] Tinyblobb{ get; set; }\npublic string Tinytextt{ get; set; }\npublic byte[] Mediumblobb{ get; set; }\npublic string Mediumtextt{ get; set; }\npublic string Longtexttm{ get; set; }\n[MaxLength(7)]\r\npublic string Enumm{ get; set; }\npublic byte[] Geometryyycol{ get; set; }\npublic byte[] Geometryyy{ get; set; }\n}";



    private void method()
    {
        var wordToFind = @"\[(Key)\]";
        var occurrences = Regex.Matches(x,wordToFind);
        var timesOfOccurrences = 0;


            foreach (Match m in occurrences)
            {
                timesOfOccurrences++;
                x = x.Insert(m.Index + m.Length-1, string.Format(",Order={0}", timesOfOccurrences));
            }

        Console.WriteLine(x);
        Console.ReadLine();
    }

只有在第一次出现单词时才能正常工作。我的正则表达式有问题吗?结果如下:

1 个答案:

答案 0 :(得分:2)

在您的代码中,您没有考虑在第一次迭代后更改输入字符串。在您使用Regex.Matches 8个字符替换第一个[Key]后,您使用[Key,Order=1]收集的索引已被转移。

我建议使用带有匹配评估程序的Regex.Replace来进行替换。它会更清晰,更不容易出错:

string x = "public class Table \n{\n[Key]\r\n[MaxLength(36)]\r\npublic string Char366{ get; set; }\npublic short? Blooean{ get; set; }\n[Key]\r\npublic bool Bit1{ get; set; }\npublic byte? Tinyinttunsigned{ get; set; }\npublic byte[] Binaryy{ get; set; }\npublic byte[] Varbinaryy{ get; set; }\npublic byte[] Blobb{ get; set; }\npublic byte[] Longblobb{ get; set; }\npublic DateTime Datetimee{ get; set; }\npublic decimal Decimall{ get; set; }\npublic double Doublee{ get; set; }\npublic short? Smallintt{ get; set; }\npublic int? Intt{ get; set; }\npublic long Bigintt{ get; set; }\npublic short Tinyintt{ get; set; }\n[Key]\r\npublic float Floatt{ get; set; }\n[MaxLength(1)]\r\npublic string Charr{ get; set; }\n[MaxLength(100)]\r\npublic string Varcharr{ get; set; }\npublic string Textt{ get; set; }\npublic string Longtextt{ get; set; }\npublic TimeSpan Timee{ get; set; }\npublic int? Mediumintt{ get; set; }\npublic long Biggintt{ get; set; }\npublic DateTime Datee{ get; set; }\npublic DateTime Timestampp{ get; set; }\npublic short Yearr{ get; set; }\npublic byte[] Tinyblobb{ get; set; }\npublic string Tinytextt{ get; set; }\npublic byte[] Mediumblobb{ get; set; }\npublic string Mediumtextt{ get; set; }\npublic string Longtexttm{ get; set; }\n[MaxLength(7)]\r\npublic string Enumm{ get; set; }\npublic byte[] Geometryyycol{ get; set; }\npublic byte[] Geometryyy{ get; set; }\n}";
var wordToFind = @"\[(Key)\]";
var occurrences = Regex.Matches(x, wordToFind);
var timesOfOccurrences = 0;
var result = Regex.Replace(x, wordToFind, m => string.Format("[{0},Order={1}]", m.Groups[1].Value, ++timesOfOccurrences));

请参阅IDEONE demo

请注意,timesOfOccurrences在匹配评估程序中递增。