我遇到一些问题,使用正则表达式与字母列表中的命名捕获组匹配字符串。
字符串本身是机场METAR(天气预报):
KLAX 050453Z 33003KT 10SM + TSRAGR HZ BR FEW010 SCT020 21/17 A3001 RMK AO2 SLP161 T02060172
这是我使用的正则表达式:
Regex MetarExpression = new Regex(@"\s(?<intensity>[\+\-VC]{1,2})?(?<descriptor>(MI|PR|BC|DR|BL|SH|TS|FZ))(?<group>(DZ|RA|SN|SG|IC|PL|GR|GS|UP|BR|FG|FU|VA|DU|SA|HZ|PY|PO|SQ|FC|SS|DS))+\s");
基本上我需要将天气组(在本例中为+ TSRAGR,HZ和BR)重写为各自的翻译,其中包括雷暴,暴雨,冰雹,阴霾,雾气等等。
以下是我目前要进行翻译的代码逻辑:
Match m = MetarExpression.Match(metar);
if (m.Success)
{
StringBuilder sb = new StringBuilder();
if(phenom.ContainsKey(m.Groups["descriptor"].Value))
{
sb.Append(phenom[m.Groups["descriptor"].Value]);
sb.Append(", ");
}
if (phenom.ContainsKey(m.Groups["intensity"].Value))
{
sb.Append(phenom[m.Groups["intensity"].Value]);
sb.Append(" ");
}
foreach (Capture cap in m.Groups["group"].Captures)
{
if (phenom.ContainsKey(cap.Value))
{
sb.Append(phenom[cap.Value]);
sb.Append(" ");
}
}
Console.WriteLine(sb);
}
字典:
static Dictionary<string, string> phenom = new Dictionary<string, string>
{
{"-", "Light"},
{"+", "Heavy"},
{"VC","In the Vicinity"},
// descriptor
{"MI","Shallow"},
{"PR","Partial"},
{"BC","Patches"},
{"DR","Low Drifting"},
{"BL","Blowing"},
{"SH","Showers"},
{"TS","Thunderstorm"},
{"FZ","Freezing"},
// precipitation
{"DZ","Drizzle"},
{"RA","Rain"},
{"SN","Snow"},
{"SG","Snow Grains"},
{"IC","Ice Crystals"},
{"PL","Ice Pellets"},
{"GR","Hail"},
{"GS","Small Hail/Snow Pellets"},
{"UP","Uknown Precipitation"},
// obscuration
{"BR","Mist"},
{"FG","Fog"},
{"FU","Smoke"},
{"VA","Volcanic Ash"},
{"DU","Widespread Dust"},
{"SA","Sand"},
{"HZ","Haze"},
{"PY","Spray"},
// other
{"PO","Well-Developed Dust/Sand Whirls"},
{"SQ","Squalls"},
{"FC","Funnel Cloud Tornado Waterspout"},
{"SS","Sandstorm"},
{"DS","Duststorm"}
};
我遇到的问题是它只捕获了第一个天气组(+ TSRAGR),而不是其他两个。
有关如何纠正此问题的任何想法?
答案 0 :(得分:0)
如果你想要捕获HZ和BR,请尝试以下,
Regex MetarExpression = new Regex(@"\s(?<intensity>[\+\-VC]{1,2})?(?<descriptor>(MI|PR|BC|DR|BL|SH|TS|FZ))(?<group>(DZ|RA|SN|SG|IC|PL|GR|GS|UP|BR|FG|FU|VA|DU|SA|HZ|PY|PO|SQ|FC|SS|DS))\s(?<group1>(DZ|RA|SN|SG|IC|PL|GR|GS|UP|BR|FG|FU|VA|DU|SA|HZ|PY|PO|SQ|FC|SS|DS))\s(?<group2>(DZ|RA|SN|SG|IC|PL|GR|GS|UP|BR|FG|FU|VA|DU|SA|HZ|PY|PO|SQ|FC|SS|DS))");
因为中间有空格字符,所以它会阻止捕获。您需要指定其他组来捕获HZ
和BR
。
它会单独捕获+TSRAGR HZ BR
。
\s(?<intensity>[\+\-VC]{1,2})?(?<descriptor>(MI|PR|BC|DR|BL|SH|TS|FZ))(?<group>(DZ|RA|SN|SG|IC|PL|GR|GS|UP|BR|FG|FU|VA|DU|SA|HZ|PY|PO|SQ|FC|SS|DS))(?<group1>(DZ|RA|SN|SG|IC|PL|GR|GS|UP|BR|FG|FU|VA|DU|SA|HZ|PY|PO|SQ|FC|SS|DS))+\s(?<group2>(DZ|RA|SN|SG|IC|PL|GR|GS|UP|BR|FG|FU|VA|DU|SA|HZ|PY|PO|SQ|FC|SS|DS))\s(?<group3>(DZ|RA|SN|SG|IC|PL|GR|GS|UP|BR|FG|FU|VA|DU|SA|HZ|PY|PO|SQ|FC|SS|DS))