如何使用C#在长字符串中搜索重复单元

如何在长字符串中搜索重复单元?
string foo = "atccuahhqtccuahh";
使用重复的子串
ccuahh
,如何使用正则表达式确定重复发生的位置? 感谢你们。但发布的代码不起作用。我在字符串中搜索任何类型的重复。任何人都可以发布经过测试的代码来帮助我吗?非常感谢。     
已邀请:
使用string.IndexOf(string,int)重载。从0开始的startIndex参数开始,您将获得第一个匹配的索引。循环,现在为参数传递找到的索引+ 1。 如果您想坚持使用Regex,请使用Match.Index属性。
var matches = Regex.Matches("atccuahhqtccuahh", "ccuahh");
var indices = matches.OfType<Match>().Select((m) => m.Index);
    
我想你正在寻找实际的reg exp。这是应该工作的:
Regex re = new Regex(@"(.+).+?1");
然而,它有点工作。为了匹配长字符串(您使用的字符串作为示例),我必须这样写:
Regex re = new Regex(@"(.{3,}).+?1");
没有明确的情人边界规范,它只匹配'a'和'hh'。 可能我想念Regex在.NET中工作的方式......     
您可以使用正则表达式进行分组。
Regex r = new Regex( @"(.+).*1" );
“(。+)”将为一个或多个字符创建一个匹配组,并表示重复的单位。这需要根据您希望重复单元具有的最小字符数进行调整。例如。将匹配组中的“+”替换为“{x,}”,其中x是最小字符数。 “ 1”匹配“(。+)”匹配的相同字符; 测试代码:
string input = "atccuahhqtccuahh";
Regex r = new Regex(@"(.+).*1");

foreach (Match match in r.Matches(input))
{
    Console.WriteLine(match.Index);
    Console.WriteLine(match);

GroupCollection groups = match.Groups;
Console.WriteLine("'{0}' repeated at positions {1} and {2}", 
    groups[0].Value,
    groups[0].Index,
    groups[1].Index);
}
    
使用LINQ怎么样?
        string text = "aafffuaffuaffuafffua";
        string search = "fff";

        var byLinq = from i in Enumerable.Range(0, text.Length)
                     where text.Length - i - search.Length > 0
                     where text.Substring(i, search.Length) == search
                     select i;
    
为什么要使用正则表达式?从一个快速的一瞥,似乎你可以使用普通的字符串方法轻松地做到这一点:
int GetIndexOfFirstRepetition(string text, string substring){
  var firstOccurrenceIndex = text.IndexOf(substring);

  var indexToSearchAfter = firstOccurrenceIndex + substring.Length;

  return text.IndexOf(substring, indexToSearchAfter);
} 
我假设子串实际上是重复的,并且通过“找到重复发生的位置”,你想要第二次出现子串,而不是第一次出现。     

要回复问题请先登录注册