C# 正在分析指定字符串上的文件

C# 正在分析指定字符串上的文件,c#,parsing,lambda,C#,Parsing,Lambda,我编写了下面的代码来解析具有以下格式的文件 请让我知道我是否可以清理代码,最好是编写lambda表达式,而不是for循环 方法中的代码已经使用StreamReader类在StringBuilder变量中包含文件内容 格式 代码 调用函数的解析器等于“其他总计” private void ParseObject(StringBuilder主体,字符串解析器) { 列表行=body.ToString().Split(Convert.ToChar(Helper.newLine)).ToList()

我编写了下面的代码来解析具有以下格式的文件

请让我知道我是否可以清理代码,最好是编写lambda表达式,而不是for循环

方法中的代码已经使用StreamReader类在StringBuilder变量中包含文件内容

格式

代码
调用函数的解析器等于“其他总计”

private void ParseObject(StringBuilder主体,字符串解析器)
{
列表行=body.ToString().Split(Convert.ToChar(Helper.newLine)).ToList();
列表页=新列表();
体长=0;
int计数器=0;
int startPosition=0;
for(int i=startPosition;i
返回的字符串如下所示:
第[0]页

第[1]页


似乎您可以使用IndexOf重载,该重载取先前的起始位置,在字符串“Other Total:”字符串后的第一个换行处将其拆分

请注意,您应该返回页面列表,否则调用将无效

Regex方法:

测试:

列表页面=新列表();
字符串模式=@“^(.*?其他总计:.*?$”;
MatchCollection matches=Regex.matches(线条、图案、,
RegexOptions.Singleline | RegexOptions.Multiline);
foreach(匹配中的匹配)
{
pages.Add(match.Groups[1].Value);
}

那么您只是将输入字符串拆分为所谓的页面中的字符串其他总计:XXXX?@Steve:没错。我甚至没有想过这样做。我会尝试一下,然后返回结果。获取长度不能是0错误。startPos=0,splitPos=-1。if语句中是否还有另一个条件splitPos!=-1.另外,我从ParseObject返回列表。但是我没有包括它,因为我正在调用ParseObject中与我的问题无关的另一个方法。尝试用您的Helper.NewLine.Using Helper.NewLine替换Environment.NewLine。非常酷,你是如何使用IndexOf的重载方法的,你是如何想到在解析器之后获得下一行的。从逻辑上讲,这就是我想做的,但我想不出如何编写代码。很高兴能帮上忙。
private void ParseObject(StringBuilder body, string parser)  
{
    List<string> lines = body.ToString().Split(Convert.ToChar(Helper.newLine)).ToList();
    List<string> pages = new List<string>();

    body.Length = 0;

    int counter = 0;
    int startPosition = 0;

    for (int i = startPosition; i < lines.Count; i++)
    {
        if (lines[i].Contains(parser))
        {

           counter = i + 1;
           for (int j = startPosition; j < counter; j++)
           {
              if (!lines[j].Contains(Helper.lineFeed))
              {
                  body.Append(string.Concat(lines[j], Helper.newLine));
              }
           }

           startPosition = counter;
           pages.Add(body.ToString());
           body.Length = 0;
        }
    }
}
private List<string> ParseObject(StringBuilder body, string parser)
{
    List<string> pages = new List<string>();

    string data = body.ToString();
    int splitPos = 0;
    int startPos = 0;
    while (true)
    {
        // Search the position of the parser string starting from the
        // previous found pos
        int parserPos = data.IndexOf(parser, splitPos);
        if (parserPos != -1)
        {
            // Now we search the position of the newline after the 
            // found parser pos
            splitPos = data.IndexOf(Environment.NewLine, parserPos);

            // Take the substring starting from the previous position up to
            // the current newline position 
            pages.Add(data.Substring(startPos, splitPos - startPos).Trim());

            // reposition to the new starting position for IndexOf
            startPos = splitPos;
        }
        else
            break;
    }
    return pages;
}
var result = ParseObject(input, "Other Total:");
List<string> pages = new List<string>();

string pattern = @"^(.*?Other Total:.*?)$";
MatchCollection matches = Regex.Matches(lines, pattern,
  RegexOptions.Singleline | RegexOptions.Multiline);

foreach (Match match in matches)
{
    pages.Add(match.Groups[1].Value);
}