Can i use regex to find the index of X?

后端 未结 7 1305
遇见更好的自我
遇见更好的自我 2020-12-15 07:45

I have a big string, and want to find the first occurrence of X, X is \"numberXnumber\"... 3X3, or 4X9...

How could i do this in C#?

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-15 08:32

    var s = "long string.....24X10     .....1X3";
    var match = Regex.Match(s, @"\d+X\d+");
    if (match.Success) {
        Console.WriteLine(match.Index); // 16
        Console.WriteLine(match.Value); // 24X10;
    }
    

    Also take a look at NextMatch which is a handy function

    match = match.NextMatch();
    match.Value; // 1X3;
    

提交回复
热议问题