我希望我的程序丢弃appGUID的值为wx或null的所有行。如何使用正则表达式来实现这一点?
我不能计算出它是正则表达式模式。请帮帮忙。
我的日志文件格式为:
INFO [com.adobe.watson.vo.BugServices] WX Edit Bug: 3494430 Server: yukon.corp.adobe.com User:xinche appGUID: null
INFO [com.adobe.watson.vo.BugServices] WX Edit Bug: 3494430 Server: yukon.corp.adobe.com User:xinche appGUID: null
INFO [com.adobe.watson.vo.BugServices] WX Edit Bug: 3494430 Server: yukon.corp.adobe.com User:xinche appGUID: null
INFO [com.adobe.watson.vo.BugServices] WX Edit Bug: 3419432 Server: yukon.corp.adobe.com User:prerelease appGUID: fcdd2153-bbdf
INFO [com.adobe.watson.vo.BugServices] WX Edit Bug: 3419432 Server: yukon.corp.adobe.com User:prerelease appGUID: fcdd2153-bbdf
INFO [com.adobe.watson.vo.BugServices] WX Edit Bug: 3419422 Server: yukon.corp.adobe.com User:prerelease appGUID: fcdd2153-bbdf
INFO [com.adobe.watson.vo.BugServices] WX Edit Bug: 3419442 Server: yukon.corp.adobe.com User:prerelease appGUID: fcdd2153-bbdf
INFO [com.adobe.watson.vo.BugServices] WX New Bug: 3494441 Server: yukon.corp.adobe.com User:bey81694 appGUID: wx
INFO [com.adobe.watson.vo.BugServices] WX New Bug: 3494441 Server: yukon.corp.adobe.com User:bey81694 appGUID: wx
INFO [com.adobe.watson.vo.BugServices] WX New Bug: 3494441 Server: yukon.corp.adobe.com User:bey81694 appGUID: wx我的代码如下:
StreamReader reader = new StreamReader(@"C:\Users\karansha\Desktop\Karan Logs\20110717.txt");
string x = reader.ReadToEnd();
List<string> users = new List<string>();
Regex regex = new Regex(@"appGUID:\s*(?<value>.*?)\s");
MatchCollection matches = regex.Matches(x);
foreach (Match match in matches)
{
var user = match.Groups["value"].Value;
if (!users.Contains(user)) users.Add(user);
}发布于 2013-03-07 14:30:08
试试这个正则表达式:
appGUID:\s*(?<value>wx|null)\s发布于 2013-03-07 14:36:11
使用不带正则表达式的LINQ解析所有用户:
var users = File.ReadAllLines("20110717.txt")
.Select(line =>
{
string guidPrefix = "appGUID:";
int index = line.IndexOf(guidPrefix);
return line.Substring(index + guidPrefix.Length + 1);
})
.Where(user => user != "null" && user != "wx")
.ToList();如果日志格式不一致(通常不应该依赖相同的格式,因为它应该是灵活的),那么正则表达式会更合适。它将处理行中appGUID位置的更改,或文本大小写的更改:
Regex regex = new Regex(@"appGUID:\s*(?<user>\S+)", RegexOptions.IgnoreCase);
var users = File.ReadAllLines("data.txt")
.Select(line => regex.Match(line))
.Where(match => match.Success)
.Select(match => match.Groups["user"].Value)
.Where(user => user != "null" && user != "wx")
.ToList();更新:实际上我会将解析部分移到单独的方法中,因为这是最有可能改变的东西。所以,你有类似这样的东西:
public List<string> GetUsersFrom(string fileName)
{
return File.ReadAllLines(fileName)
.Select(ParseUser)
.Where(u => u != null && u != "null")
.ToList();
}
private string ParseUser(string s) // Any implementation here
{
var match = Regex.Match(s, @"appGUID:\s*(?<user>\S+)");
if (!match.Success)
return null;
return match.Groups["user"].Value;
}用法:
var users = GetUsersFrom("20110717.txt").Where(u => u != "wx");发布于 2013-03-07 14:28:55
使用这个
regularexpression.replace(@"(.*)?(wx|null)\b",.....)并将其替换为string.empty,它将丢弃所有不需要的行。
StreamReader reader = new StreamReader(@"C:\Users\karansha\Desktop\Karan Logs\20110717.txt");
string x = reader.ReadToEnd();
List<string> users = new List<string>();
Regex regex = new Regex(@"(.*)?(wx|null)\b");
var newString = regex.Replace(x, String.Empty);快乐编码
https://stackoverflow.com/questions/15264434
复制相似问题