c#正则表达式匹配与字符串比较(查找最小值)
如何实现这一点,我完全被难住了。基本上,我想在METAR报告中找到上限。天花板是最小的破损或阴天层 我现在有这个(无论如何都不算多): 基本上,这会在METAR字符串中搜索BKN或OVC层并将其吐出。以此METAR读数为例:c#正则表达式匹配与字符串比较(查找最小值),c#,regex,C#,Regex,如何实现这一点,我完全被难住了。基本上,我想在METAR报告中找到上限。天花板是最小的破损或阴天层 我现在有这个(无论如何都不算多): 基本上,这会在METAR字符串中搜索BKN或OVC层并将其吐出。以此METAR读数为例: PANC 040553Z 17013G24KT 280V360 2SM FEW050 BKN019 BKN008 OVC005 14/M07 A2999 RMK AO2 SLP156 SH DSNT W-N T01390067 10167 20139 53002 我目前拥有
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)
如果有人能帮助我,我将不胜感激。基本上,您要做的是跟踪到目前为止找到的最底层,然后检查每个正则表达式匹配是否比以前低:
int lowestLayer = int.MaxValue;
MatchCollection matches = Regex.Matches(modify, @"(BKN|OVC)([0-9]{3})");
foreach (Match match in matches)
{
foreach (Capture capture in match.Captures)
{
int layer = int.Parse(capture.Value.Substring(3));
if (layer < lowestLayer)
lowestLayer = layer;
Console.WriteLine(capture.Value);
}
}
int lowestLayer=int.MaxValue;
MatchCollection matches=Regex.matches(modify,@“(BKN|OVC)([0-9]{3})”;
foreach(匹配中的匹配)
{
foreach(在match.Captures中捕获)
{
int layer=int.Parse(capture.Value.Substring(3));
if(层<最下层)
下层=层;
Console.WriteLine(capture.Value);
}
}
基本上,您要做的是跟踪到目前为止找到的最低层,然后检查每个正则表达式匹配是否比以前低:
int lowestLayer = int.MaxValue;
MatchCollection matches = Regex.Matches(modify, @"(BKN|OVC)([0-9]{3})");
foreach (Match match in matches)
{
foreach (Capture capture in match.Captures)
{
int layer = int.Parse(capture.Value.Substring(3));
if (layer < lowestLayer)
lowestLayer = layer;
Console.WriteLine(capture.Value);
}
}
int lowestLayer=int.MaxValue;
MatchCollection matches=Regex.matches(modify,@“(BKN|OVC)([0-9]{3})”;
foreach(匹配中的匹配)
{
foreach(在match.Captures中捕获)
{
int layer=int.Parse(capture.Value.Substring(3));
if(层<最下层)
下层=层;
Console.WriteLine(capture.Value);
}
}
尝试使用捕获组:
// (?<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();
/(?命名捕获数字值的组
var matches=Regex.matches(modify,@“(BKN|OVC)(?[0-9]{3})”;
//转换为IEnumerable()
var=matches.Cast()
//按解析的数字组排序
//增加了“价值”,使之生效
.OrderBy(m=>int.Parse(m.Groups[“number”].Value))
//选择字符串值
.选择(m=>m.Value)
//拿第一个(最小的)
.FirstOrDefault();
如果最小值为
null
,则未找到匹配项尝试使用捕获组:
// (?<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();
/(?命名捕获数字值的组
var matches=Regex.matches(modify,@“(BKN|OVC)(?[0-9]{3})”;
//转换为IEnumerable()
var=matches.Cast()
//按解析的数字组排序
//增加了“价值”,使之生效
.OrderBy(m=>int.Parse(m.Groups[“number”].Value))
//选择字符串值
.选择(m=>m.Value)
//拿第一个(最小的)
.FirstOrDefault();
如果最小值为
null
,则未找到匹配项如果我理解正确,您希望为最小的第二组进行整个捕获
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();
匹配。of type()
.Select(m=>new{Capture=m.Groups[0],Number=Int32.Parse(m.Groups[2]))
.OrderBy(m=…>m.Number)
.选择(m=>m.Capture)
.FirstOrDefault();
如果我理解正确,您希望对最小的第二组进行整个捕获
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();
匹配。of type()
.Select(m=>new{Capture=m.Groups[0],Number=Int32.Parse(m.Groups[2]))
.OrderBy(m=…>m.Number)
.选择(m=>m.Capture)
.FirstOrDefault();
林克是你的朋友吗
记住使用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();
MatchCollection matches=Regex.matches(modify,@“(BKN|OVC)([0-9]{3})”;
var first=(
从匹配项中的x开始。of type()
orderby int.Parse(x.Groups[2].Value)
选择x.值
).FirstOrDefault();
林克是你的朋友吗
记住使用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();
MatchCollection matches=Regex.matches(modify,@“(BKN|OVC)([0-9]{3})”;
var first=(
从匹配项中的x开始。of type()
orderby int.Parse(x.Groups[2].Value)
选择x.值
).FirstOrDefault();
+1但这将只返回最小的数字..我想op需要完全匹配次要问题是,如果没有匹配项,这将抛出异常只需更改.First()
到.FirstOrDefault
时,您将得到一个null
值,而不是最小的数字。只需检查null
,这意味着没有匹配项。使用此选项时,我会遇到两个错误:参数1:无法从“System.Text.RegularExpressions.Group”转换为“string”
和最佳重载方法“int.Parse(string)”的ch在m.Groups[“number”]
之后和结束之前有一些无效参数
@Spence add.Value
)
+1,但这只会返回最小的数..我猜op需要完全匹配小问题是,如果没有匹配,这将抛出异常just change.First()
到.FirstOrDefault
时,您将得到一个null
值,而不是最小的数字。只需检查null
,这意味着没有匹配项。使用此选项时,我会遇到两个错误:参数1:无法从“System.Text.RegularExpressions.Group”转换为“string”
和最佳重载方法“int.Parse(string)”的ch在m.Groups[“number”]
之后和结束之前有一些无效参数
@Spence add.Value
)
我强烈建议对“最小值”进行更好的定义。为什么OVC005
最小?它是基于字符串的数字部分吗?是的,它是基于字符串的数字部分。我强烈建议更好地定义“最小值”。为什么OVC005
最小?它是基于字符串的数字部分吗?是的,它是基于t