복식

2011-03-20 2 views
0

의 배열로 변환을 MatchCollection I을 MatchCollection 생산 다음 코드 가지고복식

var tmp3 = myregex.Matches(text_to_split); 

Matchestmp3에서와 같은 93.4-276.2 같은 문자열이있다. 필자가 정말로 필요로하는 것은이 MatchCollection을 double의 배열로 변환하는 것입니다. 어떻게 할 수 있습니까?

답변

5

당신은 두 배로 문자열을 변환 할 double.Parse 방법을 사용할 수 있습니다

var tmp3 = myregex.Matches(text_to_split); 
foreach (Match match in tmp3) 
{ 
    double value = double.Parse(match.Value); 
    // TODO : do something with the matches value 
} 

그리고 당신이 나 같은 LINQ와 함수형 프로그래밍 팬이라면 당신은 쓸모없는 루프를 저장하고 직접 MatchCollectionIEnumerable<double>로 변환 할 수 있습니다 : 정적 배열을 필요한 경우

var tmp3 = myregex.Matches(text_to_split); 
var values = tmp3.Cast<Match>().Select(x => double.Parse(x.Value)); 

및 추가 .ToArray() 호출이 필요할 수 있습니다

당신이 안전 변환을 원하는 경우
var tmp3 = myregex.Matches(text_to_split); 
var values = tmp3.Cast<Match>().Select(x => double.Parse(x.Value)).ToArray(); 

당신은 double.TryParse 방법을 사용하지만 수있는 정규 표현식이 충분하고 문자열이 확인을해야 적절한 형식으로되어 있음을 보장 한 경우.

+0

이것은 정확히 내가 찾고있는 것입니다. –

관련 문제