0

私はこれを達成する方法に完全に困惑しています。基本的には、METAR レポートで天井を見つけたいと思っています。天井は、最小の壊れたまたは曇った層です。

私は現在これを持っています(決して多くはありません):

MatchCollection matches = Regex.Matches(modify, @"(BKN|OVC)([0-9]{3})");
foreach (Match match in matches)
{
    foreach (Capture capture in match.Captures)
    {
        // compare broken and overcast layers to find smallest value and set as the ceiling
        Console.WriteLine(capture.Value);
    }
}

基本的に、これは BKN または OVC レイヤーの METAR 文字列を検索し、それらを吐き出します。たとえば、次の METAR 測定値を見てください。

PANC 040553Z 17013G24KT 280V360 2SM FEW050 BKN019 BKN008 OVC005 14/M07 A2999 RMK AO2 SLP156 SH DSNT W-N T01390067 10167 20139 53002

私が現在持っているコードは、BKN019、BKN008、および OVC005 を吐き出します。私がしなければならないことは、これらの値の最小のものを選ぶことです (この場合、OVC005 になります)。

誰かが私を助けることができれば、私はそれを大いに感謝します.

4

4 に答える 4

3

キャプチャ グループを使用してみてください。

// (?<number> names the group that captures the number value
var matches = Regex.Matches(modify, @"(BKN|OVC)(?<number>[0-9]{3})");

// cast to IEnumerable<Match>()
var smallest = matches.Cast<Match>()
    // order by the parsed number group
    // Added `.Value` to make this work
    .OrderBy(m => int.Parse(m.Groups["number"].Value))
    // Select the string value
    .Select(m => m.Value)
    // take the first (the smallest)
    .FirstOrDefault();

最小の場合null、一致するものは見つかりません

于 2013-07-04T18:55:20.883 に答える
1

私の理解が正しければ、最小の 2 番目のグループのキャプチャ全体を取得する必要があります。

matches.OfType<Match>()
       .Select(m => new { Capture = m.Groups[0], Number = Int32.Parse(m.Groups[2]) })
       .OrderBy(m =­> m.Number)
       .Select(m => m.Capture)
       .FirstOrDefault();
于 2013-07-04T18:56:35.290 に答える
0

Linqあなたの友達です

覚えてusing System.Linq

MatchCollection matches = Regex.Matches(modify, @"(BKN|OVC)([0-9]{3})");
var first = (
             from x in matches.OfType<Match>()
             orderby int.Parse(x.Groups[2].Value)
             select x.Value
            ).FirstOrDefault();
于 2013-07-04T19:04:52.537 に答える