在C#中检查以相同字母开头和结尾的单词

我写的代码只打印那些大于长度为5的单词。现在我要打印以相同字母开头和结尾的单词。 请注意,它应该使用正则表达式库在C#中完成。 用于打印大于5的单词的代码。

String str="programing world is nooooooot funnnnnnn"; Regex reg=new Regex("^[a-zA-Z_\\w]\\w*$"); String[] words=str.Split(' '); for(int i=0;i<words.length;i++) { String temp=words[i]; if(Regex.IsMatch(temp,@"[a-zA-Z]{5}")) Console.Writeline(temp); } 

您可以使用正则表达式捕获组。

 if(Regex.IsMatch(temp,@"^([a-zA-Z])[a-zA-Z]{3,}\1$")) 

它应匹配以相同字母开头和结尾的单词,并且该单词必须包含至少5个字母。 对于超过5个字母,只需将数字3更改为4。