开发者

C# Programming How to Tokenize Spacing and "," through Regular Expression?

开发者 https://www.devze.com 2023-01-26 01:31 出处:网络
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

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);
        }
    }
}
}


You can use

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

instead of regular expressions.


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,]";
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号