I am trying to extract data from text. It is larger text. For better understanding. I have simple class Test with one atribute result, which is enum.
class Test{
private Result result;
public Test(string text){
this.Result = FindProblem(text);
}
......
public enum Result
{
SUCCESS = 1,
FAIL = 2,
NONE = 3
}
}
Then I have text:
Today at 01:05: (NAME) Failed Backup - The operation encountered an error. (NAME)
Failed when backing up: A file was not found (NAME)
DISK_OPEN_ERROR
Today at 01:04: (NAME) Successful Backup - Backed 42,73 MB (compressed to 7,32 MB).(Duration: 1 minute)
Today at 00:59: (NAME) Successful Backup - Backed 3,41 GB (compressed to 379,17 MB).(Duration: 4 minutes)
And I want each row to return a Test object with results like: result: Failed or result: Succesful.
Method for returning all objects from text
public Result FindResult(string text)
{
var splitText = text.Split("\n").ToList();
Result r = Result.NONE;
foreach (var item in splitText)
{
if (item.Contains("Failed"))
{
r = Result.FAIL;
}
else
{
r = Result.SUCCESS;
}
}
return r;
}
But it returns only result SUCCESS. Expected output
result:FAIL
result:SUCCESS
result:SUCCESS
1条答案
按热度按时间dldeef671#
您可能要做的是在至少2个换行符上拆分,然后使用Select并检查是否包含“Failed”来Map列表中的项目
如果是,则返回
Result.FAIL
,否则返回Result.SUCCESS
,因此函数的返回类型为List<Result>
然后可以循环返回列表中的项
输出量
请参见C# demo。