Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#从文件读取-整数,忽略所有其他内容_C# - Fatal编程技术网

C#从文件读取-整数,忽略所有其他内容

C#从文件读取-整数,忽略所有其他内容,c#,C#,我有一个config.txt文件,看起来像这样 some text here = 5 another text line here = 4 with random garbage some other line = 2 5 some number 12 7 foo bar 9 if (file_to_read.is_open()) { file_to_read.ignore(numeric_limits<streamsize>::max(), '=');

我有一个config.txt文件,看起来像这样

some text here = 5
another text line here = 4 with random garbage
some other line = 2 5 some number 12
7
foo bar 9
if (file_to_read.is_open()) {
        file_to_read.ignore(numeric_limits<streamsize>::max(), '='); 
        while (file_to_read >> input) { 
            values.push_back(input);
            file_to_read.ignore(numeric_limits<streamsize>::max(), '=');
        }
        file_to_read.close();
为了澄清,只有“=”后面的数字。只是在“=”后面显示的一个数字

我想只提取“=”之后的整数。因此,在本例中,输出应该如下所示

5
4
2
我以前在C++中使用过类似的东西

some text here = 5
another text line here = 4 with random garbage
some other line = 2 5 some number 12
7
foo bar 9
if (file_to_read.is_open()) {
        file_to_read.ignore(numeric_limits<streamsize>::max(), '='); 
        while (file_to_read >> input) { 
            values.push_back(input);
            file_to_read.ignore(numeric_limits<streamsize>::max(), '=');
        }
        file_to_read.close();
if(文件\u到\u read.is\u open()){
文件读取。忽略(数值限制::max(),'=');
而(文件读取>>输入){
值。推回(输入);
文件读取。忽略(数值限制::max(),'=');
}
文件_to_read.close();
我想了解你在C#中是如何做到这一点的,如果有任何文档我可以参考

我们开始:

string input =
@"some text here = 5
another text line here = 4 with random garbage
some other line = 2 5 some number 12
7
foo bar 9";

var matches = Regex.Matches(input, @"(?:.*(?<==).*)(\d+)");
foreach(Match match in matches)
{
    Console.WriteLine(match.Groups[1]);
}
对于Regex,发生的情况如下: 任意符号的非选择性查找任意次数后跟“=”号:

(?:.*(?<==).*)

不客气。=)

使用正则表达式,过程并不太复杂。在读取流时进行过滤似乎太复杂了。只需执行以下操作:

  • 将文本文件读入单个字符串
  • 使用正则表达式搜索包含文本
    “=”
    字符串,后跟数字字符
    \d+
    的模式
  • 将表达式的数字部分放在圆括号中,可以将其捕获为组#1,您可以从
    Match
    对象中提取为
    m.Groups[1]。Value
  • 每个找到的整数都会被解析并添加到整数列表中
  • 操作完成后,返回列表

    public static List<Int32> ExtractInts(String filePath)
    {
        String input = File.ReadAllText(filePath);
        List<Int32> ints = new List<Int32>();
        Regex r = new Regex(" = (\\d+)");
        MatchCollection mc = r.Matches(input);
        foreach(Match m in mc)
            ints.Add(Int32.Parse(m.Groups[1].Value));
        return ints;
    }
    
    公共静态列表ExtractInts(字符串文件路径)
    {
    字符串输入=File.ReadAllText(文件路径);
    List ints=新列表();
    正则表达式r=新正则表达式(=(\\d+);
    MatchCollection mc=r.Matches(输入);
    foreach(在mc中匹配m)
    ints.Add(Int32.Parse(m.Groups[1].Value));
    返回整数;
    }
    

输出不应该是5和4吗?只需使用
=(\d+)
正则表达式…@Albert Alonso,是的,应该是5和4。对输入错误表示歉意。这是正确的@RufusL。关于如何使用c#读取文本文件,如何对每个
执行
执行
,或者
循环(处理文件数据)时执行
,以及如何将字符与不同的值进行比较。到目前为止,您尝试了什么?如果您向我们说明您遇到了什么问题,将更容易提供帮助。如果您使用捕获组,则不需要查找。您只需在\d之前使用文字“=”。