我正在尝试从一个具有大量格式的字符串中提取不同长度的整数。所涉及的字符串可能如下所示:
string s = "Hallo (221122321 434334 more text3434 even mor,34343 343421.343sf 343";
我要的输出是一个数组,包括以下内容:
{221122321,434334,3434,34343,343421,343,343}
var result = new Regex(@"\d+").Matches(s)
.Cast<Match>()
.Select(m => Int32.Parse(m.Value))
.ToArray();
string result = "";
foreach (string str in s)
{
int number;
if (int.TryParse(str, out number))
result += s;
else
result += ",";
}
+1
- Soner Gönül.OfType<Match>
更改为.Cast<Match>
,以更好地表明我期望所有匹配项实际上都是Match
类型(它们不能是任何其他类型)。 - Matthew Watsonm => m.Value
改成m => Int32.Parse(m.Value)
吗?这样你就可以得到一个整数数组,而不是字符串数组了。(期望输出中没有引号。) - Idle_Mind