如何从格式化的字符串中提取指定的文本 [英] How to extract specified text from a formatted string

查看:151
本文介绍了如何从格式化的字符串中提取指定的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个看起来像这样的日志文件

12/19/08 10:05:09.367 |信息| ComputerName:COMP-1C6751B9777897870
12/19/08 10: 05:09.367 |信息| UserDomainName:SCMSNABCDEF
12/19/08 10:05:09.367 |信息| UserInteractive:True
2012年12月19日10:05:09.367 |信息| UserInteractive:False
12/19/08 10:05:09.383 |系统| PrivilegedProcessor时间:00:00:00.2656250
12/19/08 10:05:09.383 |系统| TotalProcessor时间:00:00:02.0781250
12/19 / 08 10:05:09.539 |服务| Ib Msg: http://CMS.ServiceContracts/2006/10/IRefItemService/GetMultipleListComplexRefTypeResponse ,大小:113207,时间:107毫秒
12/19/08 10:05:09.757 |服务| Ib Msg: HTTP://CMS.ServiceContracts/2006/10/IRefItemService/GetMultipleListComplexRefTypeResponse ,大小:78159,时间:73毫秒
12/19/08 10:05:10.070 |服务| Ib Msg: http ://CMS.ServiceContracts/2006/10/IRefItemService/GetMultipleListComplexRefTypeResponse ,大小:213881,时间:128毫秒..............
...... ..........

从这个文件中我想提取ComputerName,UserDomainName,UserInteractive,UserInteractive的值,然后把它放到标签和网格中。

可以任意一个建议我怎么做???

提前谢谢

解决方案


如果日志文件是small one可以将整个文件读入单个字符串,并使用正则表达式来提取所需信息。另一种方法是逐行读取文件并检查每一行以查看它是否包含您感兴趣的信息。我正在采用带有正则表达式的逐行方法。假设您有一个名为logFile的流对象,您正在从中读取日志。






























Regex targetExpression = new 正则表达式(@ "([^ |] * \\ \\ |){2}(?< label> [^:] *):(?< value>。*)" );
string nextLine;
((nextLine = logFile.ReadLine())!= null
{
匹配m = targetExpression.Match(nextLine);
if (m.Success)
{
string label = m.Groups [ " label" ]。
string val = m.Groups [ " value" ]值;
}
}

Hi
I have a log file which looks like this 

12/19/08 10:05:09.367|Information |ComputerName: COMP-1C6751B9777897870
12/19/08 10:05:09.367|Information |UserDomainName: SCMSNABCDEF
12/19/08 10:05:09.367|Information |UserInteractive: True
12/19/08 10:05:09.367|Information |UserInteractive: False
12/19/08 10:05:09.383|System      |PrivilegedProcessorTime: 00:00:00.2656250
12/19/08 10:05:09.383|System      |TotalProcessorTime: 00:00:02.0781250
12/19/08 10:05:09.539|Service     |Ib Msg: http://CMS.ServiceContracts/2006/10/IRefItemService/GetMultipleListComplexRefTypeResponse, Size: 113207, Time: 107 ms
12/19/08 10:05:09.757|Service     |Ib Msg: http://CMS.ServiceContracts/2006/10/IRefItemService/GetMultipleListComplexRefTypeResponse, Size: 78159, Time: 73 ms
12/19/08 10:05:10.070|Service     |Ib Msg: http://CMS.ServiceContracts/2006/10/IRefItemService/GetMultipleListComplexRefTypeResponse, Size: 213881, Time: 128 ms ..............
................

From this file i want to extract values of ComputerName, UserDomainName, UserInteractive, UserInteractive and then put it into lables and a grid.

can any one suggest me how to do that ???

Thanks in advance

解决方案


If the log file is small one could read the entire file into a single string and use regular expressions to pull out desired information.  The other approach is to read the file line-by-line and examine each line to see if it has information in which you have interest.   I'm going with a line-by-line approach  with a regular expression.  Suppose you have a stream object named logFile from which you are reading your log.

            Regex targetExpression = new Regex(@"([^|]*\|){2}(?<label>[^:]*):(?<value>.*)");  
 
            string nextLine;  
            while((nextLine=logFile.ReadLine())!=null)  
            {  
                Match m = targetExpression.Match(nextLine);  
                if(m.Success)  
                {  
                    string label = m.Groups["label"].Value;  
                    string val = m.Groups["value"].Value;  
                }  
            } 


这篇关于如何从格式化的字符串中提取指定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆