c#中如何循环读取文本文件中 2个指定字符之间的内容?

时间:2023-02-11 07:44:58
比如我本地有一个文本文档,里面的内容如下:
@@@               3  3        ###@@@               8  8        ###@@@               8  8        ###@@@               0  0        ###@@@               6  6        ###@@@               6  6        ###@@@               1  1        ###@@@               6  6        ###@@@               3  3        ###@@@               1  1        ###@@@               0  0        ###@@@               9  9        ###@@@               5  5        ###@@@               7  7        ###@@@               9  9        ###@@@               8  8        ###

我如何循环读取@@@和###之间的内容,并将其对应存入字符串数组中,本人正则学的不好,希望有大佬多多指教!!不胜感激!!! 

4 个解决方案

#1


c#中如何循环读取文本文件中 2个指定字符之间的内容?

#2


        string text = @"@@@               3  3        ###@@@               8  8        ###@@@               8  8        ###@@@               0  0        ###@@@               6  6        ###@@@               6  6        ###@@@               1  1        ###@@@               6  6        ###@@@               3  3        ###@@@               1  1        ###@@@               0  0        ###@@@               9  9        ###@@@               5  5        ###@@@               7  7        ###@@@               9  9        ###@@@               8  8        ###";
        rx = new Regex("@@@(.*?)###");
        MatchCollection matches = rx.Matches(text);
        foreach (Match m in matches)
        {
            Response.Write(string.Format("<div>{0}</div>",m.Groups[1].Value));
        }

#3


大佬,你的方法可行,我想问一下,这个规则是怎么个意思,怎么选取的 
@"(?<=@@@).*?(?=###)")
                .Cast<Match>().Select(s => s.Value.Trim()).ToArray();

#4


引用 1 楼 exception1992的回复:
c#中如何循环读取文本文件中 2个指定字符之间的内容?

大佬,现在如果在整个字符串前和尾加上WWW,MMM,该如何修改代码呢,还是读取之前的内容?如下所示:
WWW@@@               1  1        ###@@@               2  2       ###@@@               3  3        ###@@@               4  4        ###@@@               5  5        ###@@@               6  6        ###@@@               7  7        ###@@@               8  8        ###@@@               9  9        ###@@@               10  10        ###@@@               11  11        ###@@@               12  12       ###@@@               13  13        ###@@@               14  14        ###@@@               15  15        ###@@@               16  16        ###@@@               17  17        ###@@@               18  18        ###@@@               19  19        ###@@@               20  20        ###@@@               21  21        ###@@@               22  22       ###@@@               23  23        ###MMM

#1


c#中如何循环读取文本文件中 2个指定字符之间的内容?

#2


        string text = @"@@@               3  3        ###@@@               8  8        ###@@@               8  8        ###@@@               0  0        ###@@@               6  6        ###@@@               6  6        ###@@@               1  1        ###@@@               6  6        ###@@@               3  3        ###@@@               1  1        ###@@@               0  0        ###@@@               9  9        ###@@@               5  5        ###@@@               7  7        ###@@@               9  9        ###@@@               8  8        ###";
        rx = new Regex("@@@(.*?)###");
        MatchCollection matches = rx.Matches(text);
        foreach (Match m in matches)
        {
            Response.Write(string.Format("<div>{0}</div>",m.Groups[1].Value));
        }

#3


大佬,你的方法可行,我想问一下,这个规则是怎么个意思,怎么选取的 
@"(?<=@@@).*?(?=###)")
                .Cast<Match>().Select(s => s.Value.Trim()).ToArray();

#4


引用 1 楼 exception1992的回复:
c#中如何循环读取文本文件中 2个指定字符之间的内容?

大佬,现在如果在整个字符串前和尾加上WWW,MMM,该如何修改代码呢,还是读取之前的内容?如下所示:
WWW@@@               1  1        ###@@@               2  2       ###@@@               3  3        ###@@@               4  4        ###@@@               5  5        ###@@@               6  6        ###@@@               7  7        ###@@@               8  8        ###@@@               9  9        ###@@@               10  10        ###@@@               11  11        ###@@@               12  12       ###@@@               13  13        ###@@@               14  14        ###@@@               15  15        ###@@@               16  16        ###@@@               17  17        ###@@@               18  18        ###@@@               19  19        ###@@@               20  20        ###@@@               21  21        ###@@@               22  22       ###@@@               23  23        ###MMM