Regex is behaving lazy, should be greedy

后端 未结 3 1746
感动是毒
感动是毒 2021-02-20 15:12

I thought that by default my Regex would exhibit the greedy behavior that I want, but it is not in the following code:

 Regex keywords = new Reg         


        
3条回答
  •  别跟我提以往
    2021-02-20 15:45

    It looks like you're trying to word break things. To do that you need the entire expression to be correct, your current one is not. Try this one instead..

    new Regex(@"\b(in|int|into|internal|interface)\b");
    

    The "\b" says to match word boundaries, and is a zero-width match. This is locale dependent behavior, but in general this means whitespace and punctuation. Being a zero width match it will not contain the character that caused the regex engine to detect the word boundary.

提交回复
热议问题