在某个短语后获取字符串/文本

我需要从日志文件中获取“运行模块”之后的词。“运行模块”之后的单词总是以句号 (.) 结尾,我需要将此单词插入数组中,以便以后需要时访问。


日志中的示例文本是:


25.5.2018 10:42:35.621 状态消息 0884:正在运行模块 SamplesSim。


25.5.2018 10:42:35.621 状态消息 0120:Data Input: Opening: T:\RCS\BiggieStatementsOn\PRINT\INPUTJACK\FCA_HomeBandAccounts_23052018.csv。(数据输入1)


我试过使用indexOf(),但我一直将“运行”作为我的输出。


这是我的代码:


string consoleOutput = compiler.StandardOutput.ReadToEnd();


startIndex = consoleOutput.IndexOf("Running module");

string[] ModuleParams;

string runningMod = "";


if (startIndex != -1)

{

    lastIndex = startIndex +3;

    if (lastIndex != -1)

    {

        ModuleParams = consoleOutput

                           .Substring(startIndex, lastIndex - startIndex)

                           .Split(' ');


        runningMod = ModuleParams[0];

                     //.Substring(4).Replace("Running module", "").Trim();

    }                                

    Console.WriteLine(runningMod);

}


翻过高山走不出你
浏览 158回答 3
3回答

繁花如伊

我更正了你的代码,现在它输出 SamplesSimusing System;public class Program{    public static void Main()    {            string consoleOutput = @"                25.5.2018 10:42:35.621 Status message 0884:Running module SamplesSim.                25.5.2018 10:42:35.621 Status message 0120:Data Input: Opening: T:\RCS\BiggieStatementsOn\PRINT\INPUTJACK\FCA_HomeBandAccounts_23052018.csv. (DataInput1)            ";            string wordToFind = "Running module";            var startIndex = consoleOutput.IndexOf(wordToFind);            string[] ModuleParams;            string runningMod = "";            if (startIndex != -1)            {                var lastIndex = startIndex + wordToFind.Length;                if (lastIndex != -1)                {                    ModuleParams = consoleOutput                                       .Substring(startIndex+wordToFind.Length, consoleOutput.Length - (startIndex+wordToFind.Length))                        .Split(new char[]{' ','\n','.'}, StringSplitOptions.RemoveEmptyEntries);                    runningMod = ModuleParams[0];                                 //.Substring(4).Replace("Running module", "").Trim();                }                                                Console.WriteLine(runningMod);            }    }}
打开App,查看更多内容
随时随地看视频慕课网APP