c# - Regex match with dictionary key -
i'm expanding upon asked question.
before explain, here c# code:
static dictionary<string, string> phenom = new dictionary<string, string> { {"-", "light"}, {"\\+", "heavy"}, {"\bvc\b","in 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"} }; public static string process(string metar) { metar = regex.replace(metar, "(?<=a[0-9]{4}).*", ""); stringbuilder _string = new stringbuilder(); var results = result in phenom regex.match(metar, result.key, regexoptions.singleline).success select result; foreach (var result in results) { if (result.key == "dz" || result.key == "ra" || result.key == "sn" || result.key == "sg" || result.key == "pl") { switch (result.key) { case "+": _string.append("heavy "); break; case "-": _string.append("light"); break; case "vc": _string.append("in vicinity "); break; default: break; } } _string.appendformat("{0} ", result.value); } homecoming _string.tostring(); }
basically, code parses weather phenomenons in airport metar weather report. take next metar example:
kama 230623z auto 05016kt 7sm +tsragr few070 bkn090 ovc110 16/14 a3001 rmk ao2 pk wnd 06026/0601 ltg dsnt e-sw rab04 tsb02e17 p0005 t01560144
notice +tsragr...this portion want focus on. code have works fine...but not close want. spits out this: "heavy thunderstorm rain hail".
the actual decoding differs slightly. referenced this manual (see 5th page).
the intensity indicators (+ , -) come before first precipitation. in above metar, ra. so, ideally should spit out "thunderstorm heavy rain hail", instead. however, not. there other times when phenomena not intense...sometimes may -ra, in case should homecoming "light rain".
also note, manual referenced says, intensity identifiers not coded ic, gr, gs or precipitation types, explains effort @ checking key value before appending intensity, failed.
if point me in right direction on how append intensity identifiers in right location, i'd much appreciate it.
tl;dr...basically, code logic decide set prefixes before specific items dictionary list.
given construction of code, recommend pre-parsing input string "move" intensity indicators after terms don't modify, words come out in order makes sense.
then, might consider adding few keys dictionary cover +/- versions of each type of precipitation can have intensity indicator, overall code simplified.
c# regex