C# 从字符串regex获取数字

C# 从字符串regex获取数字,c#,regex,C#,Regex,我想把字符串上的所有数字都记在c上 例如: sadsad 2 fsdg 4 njnjk 5 njnsdf 9 jytjtj 我想去 那些数字,我该怎么做? 谢谢这里记录了正则表达式: 要测试regex,这个网站对我很有帮助: 假设您想要一个包含Int32值的列表,它可能是这样的 string pattern = "[0-9]"; string input = "sadsad 2 fsdg 4 njnjk 5 njnsdf 9 jytjtj";

我想把字符串上的所有数字都记在c上

例如:

sadsad 2 fsdg 4 njnjk 5 njnsdf 9 jytjtj
我想去

那些数字,我该怎么做?
谢谢这里记录了正则表达式:

要测试regex,这个网站对我很有帮助:

假设您想要一个包含Int32值的列表,它可能是这样的

        string pattern = "[0-9]";
        string input = "sadsad 2 fsdg 4 njnjk 5 njnsdf 9 jytjtj";
        Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
        MatchCollection result = rgx.Matches(input);

        var resultList = new List<Int32>(); 
        foreach (Match match in result)
        {
            resultList.Add(Int32.Parse(match.Value));
        }
字符串模式=“[0-9]”;
字符串输入=“sadsad 2 fsdg 4 njnjk 5 njnsdf 9 jytjtj”;
Regex rgx=新的Regex(模式,RegexOptions.IgnoreCase);
MatchCollection结果=rgx.Matches(输入);
var resultList=新列表();
foreach(结果匹配)
{
Add(Int32.Parse(match.Value));
}
string text=“sadsad 2 fsdg 4 njnjk 5 njnsdf 9 jytjtj 123 456 78 9”;
Regex Regex=new Regex(@“\d{1,5}”/*最多5位*/,RegexOptions.Compiled);
List numberList=regex.Matches(text.Cast().Select(m=>int.Parse(m.Value)).ToList();

该商品可能重复,但如果我有两位数字的号码不起作用…(您只使用一位数字进行编码),我如何获得该号码11?谢谢
string text = "sadsad 2 fsdg 4 njnjk 5 njnsdf 9 jytjtj 123 456 78 9";
            Regex regex = new Regex(@"\d{1,5}" /* up to 5 digits */, RegexOptions.Compiled);

            List<int> numberList = regex.Matches(text).Cast<Match>().Select(m => int.Parse(m.Value)).ToList();