Can i use regex to find the index of X?

后端 未结 7 1292
遇见更好的自我
遇见更好的自我 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:31

    this may help you

       string myText = "33x99 lorem ipsum  004x44";
    
        //the first matched group index
        int firstIndex =  Regex.Match(myText,"([0-9]+)(x)([0-9]+)").Index;
    
        //first matched "x" (group = 2) index 
        int firstXIndex =  Regex.Match(myText,"([0-9]+)(x)([0-9]+)").Groups[2].Index;
    

提交回复
热议问题