Perhaps this could help. This function locates all the occurrences of the text you're looking for:
private List<Match> FindOccurrences( string SourceText, string Expression )
{
List<Match> Result = new List<Match>();
Regex Parser = new Regex( Expression, RegexOptions.Compiled );
Match Match = Parser.Match( SourceText );
while (Match.Success)
{
Result.Add( Match );
Match = Match.NextMatch();
}
return Result;
}
You can call the function simply like this:
List<Match> Matches = FindOccurrences( "This contains boy here and again contains boy here", "boy" );
The property
Index
of the objects of type
Match
contains the position into the input string.