vicage
vicage

Reputation: 75

Series of Starting and Ending Index for Specific pattern of substring in C#

string str = "AAA  AAAAA    AA"

I need to figure out starting and Ending index of AAA,AAAAA,AA. In the above example, the index are (1,3),(6,10),(14,16). Is it possible to achieve this by using regex

Upvotes: 1

Views: 175

Answers (1)

spender
spender

Reputation: 120450

According to @HamZa's comment: You could do something like this:

var r = new Regex(@"(\S)(?:\S*(\S))?");
var input = "AAA  AAAAA    AA";
var clusterPositions = r.Matches(input).Cast<Match>()
                        .Select(m => new{start = m.Index, 
                                           end = m.Index + m.Length});

Upvotes: 1

Related Questions