C# 编程如何标记间距和“,”通过正则表达式?

发布于 2024-10-04 05:31:17 字数 1220 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不顾 2024-10-11 05:31:17

您可以使用

value.Split(new Char[]{',', ' '}, StringSplitOptions.RemoveEmptyEntries) 

代替
正则表达式。

You can use

value.Split(new Char[]{',', ' '}, StringSplitOptions.RemoveEmptyEntries) 

instead of
regular expressions.

安穩 2024-10-11 05:31:17

正如 Jens 指出的,您可以只使用 String.Split 方法。然而,正则表达式将是这样的:

String rex = @"[\s,]";

As Jens pointed out, you can get away with just using the String.Split method. However, the regular expression will be like this:

String rex = @"[\s,]";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文