TjDillashaw
TjDillashaw

Reputation: 817

Match.index not work correctly

Im trying to find [Key] word in my text and add something to it.

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();
    }

It works fine only with the first occurrence of the word. Is there something wrong with my regex? Here is result:

Upvotes: 1

Views: 308

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626903

In your code, you did not take into account that the input string is changed after the first iteration. The indices you collected with Regex.Matches have been shifted after you replaced the first [Key] with [Key,Order=1] 8 characters.

I suggest using a Regex.Replace with a match evaluator to do the replacing. It will be much cleaner and less error prone:

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));

See the IDEONE demo

Note that timesOfOccurrences is incremented inside the match evaluator.

Upvotes: 3

Related Questions