我有一个 C# 程序,它接收日志字符串并尝试将其标记为各种数组。
示例中使用的字符串是我需要的“Tue Oct 26 2010 23:48:54,664,macb,d/drwxrwxrwx,0,0,33-144-1,C:/WINDOWS/system32/ras”将 Spacing (' ') 和 ","分隔成数组。运行我的程序的结果是这样的:
Tue Oct 26 2010 23:48:54
664
macb
d/drwxrwxrwx
0
0
33-144-1
C:/WINDOWS/system32/ras
这个结果只是部分正确,因为“,”被过滤,而不是间距 (' ')。因此我也需要过滤掉时间 23:48:54。有人可以请告诉我代码吗?谢谢!
我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
namespace Testing
{
class Program
{
static void Main(string[] args)
{
//System.Collections.Generic.IEnumerable<String> lines = File.ReadLines
("C:\\Test\\ntfs2.txt");
String value = "Tue Oct 26 2010 23:48:54,664,macb,d/drwxrwxrwx,0,0,33-144-
1,C:/WINDOWS/system32/ras";
//
// Split the string on line breaks.
// ... The return value from Split is a string[] array.
//
String rex = @"(\t)|,";
String[] lines = Regex.Split(value, rex);
foreach (String line in lines)
{
Console.WriteLine(line);
}
}
}
}
请您参考如下方法:
你可以使用
value.Split(new Char[]{',', ' '}, StringSplitOptions.RemoveEmptyEntries)
代替 常用表达。
