Friday, April 13, 2012

Convert MatchCollection into List

private void ConvertMatchCollectionIntoList()
{
string test = "1a2bc23AB4C15";
//string regex = "(?a)|(?b)|(?c)]";
string regex = "[\\d]+";
Regex regNoCase = new Regex(regex, RegexOptions.IgnoreCase);
Regex regCase = new Regex(regex);

MatchCollection matchNoCase = regNoCase.Matches(test);
MatchCollection matchCase = regCase.Matches(test);


List lstIndex = matchNoCase.OfType().Where(m => m.Success).ToList();
lstIndex.Sort(CompareLst);

}

public int CompareLst(Match x, Match y)
{

int iFirst =0;
int.TryParse(x.Value,out iFirst);

int iSecond;
int.TryParse(y.Value,out iSecond);
return iFirst.CompareTo(iSecond);
}

No comments:

Followers

Link