このようにするのはどうですか。基本的に、以前に<に遭遇した場合にのみ、>をカウントする必要があります。または別の言い方をします。<を積み重ねて、>に遭遇したときにそれぞれ1つずつ使用します。
string test = "You are pretty <lady> but that <girl> is prettier <than> you.";
int startcount = 0;
int paircount = 0;
foreach( char c in test ){
if( c == '<' )
startcount++;
if( c == '>' && startcount > 0 ){
startcount--;
paircount++;
}
}
//paircount should now be the value you are after.
編集
<<< >>>は1ではなく3を数える必要があると思ったので、上記の簡単な修正が必要です。<<< >>>を1つだけとしてカウントするには、これに変更します
string test = "You are pretty <lady> but that <girl> is prettier <than> you.";
bool foundstart = false;
int paircount = 0;
foreach( char c in test ){
if( c == '<' )
foundstart = true;
if( c == '>' && foundstart ){
foundstart = false;
paircount++;
}
}
//paircount should now be the value you are after.