C# 编程如何标记间距和“,”通过正则表达式?
我有一个 C# 程序,它接收日志字符串并尝试将其标记为各种数组。
用于示例的字符串为“Tue Oct 26 2010 23:48:54,664,macb,d/drwxrwxrwx,0,0,33-144-1,C:/WINDOWS/system32/ras”,我需要两个间距( ' ') 和 "," 被分隔成数组。运行我的程序的结果将是这样的:
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);
}
}
}
}
I have a C# program which takes in a log string and tries to tokenize it into various arrays.
The string used for the example would be "Tue Oct 26 2010 23:48:54,664,macb,d/drwxrwxrwx,0,0,33-144-1,C:/WINDOWS/system32/ras" which I need both Spacing (' ') and "," to be seperated into arrays. The results from running my programs would be this:
Tue Oct 26 2010 23:48:54
664
macb
d/drwxrwxrwx
0
0
33-144-1
C:/WINDOWS/system32/ras
This result is only partially right as the "," is being filtered but not the spacing (' '). Therefore I need the time 23:48:54 to be filtered out as well. Can someone please advise me on the codes please? Thanks!
My Codes:
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);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
代替
正则表达式。
You can use
instead of
regular expressions.
正如 Jens 指出的,您可以只使用 String.Split 方法。然而,正则表达式将是这样的:
As Jens pointed out, you can get away with just using the String.Split method. However, the regular expression will be like this: