C# C中的拆分问题#

C# C中的拆分问题#,c#,parsing,string,C#,Parsing,String,以下是在文本文件中找到的日志数据集 ********************************************************************************** **2008/04/06** 00:35:35 193111 1008 O 9448050132# 74 **2008/04/06** 00:35:35 193

以下是在文本文件中找到的日志数据集

**********************************************************************************
**2008/04/06** 00:35:35 193111               1008                O          9448050132# 74                               
**2008/04/06** 00:35:35 193116               1009                 O          9448050132# 74                               
 **12/15/2008**   8:36AM 106  01 090788573                             00:01'23" ..06  
**10/10/2008** 14:32:32 4400 4653  00:00:56 26656            0    0           OG AL# 
 &       0000    0000                                      
N 124 00 8630    T001045 **10/16** 05:04 00:01:02 A 34439242360098
***************************************************************************************
我只需要从以上所有行中提取日期详细信息(可能是200/04/06或10/16),并将其显示在文本框中

如果数据按如下顺序排列,我知道如何分隔日期

***************************************************************************************
10/10/2008 14:32:32 4400 4653  00:00:56 26656            0    0           OG AL#

10/10/2008 14:33:29 4400 4653  00:00:02 26656434         0    0           OG LL#

10/10/2008 14:33:31 4400 4653  00:00:11 26656434         0    0           OG LL#
***************************************************************************************
其代码为:

        StreamReader rr = File.OpenText("C:/1.txt");
        string input = null;
        while ((input = rr.ReadLine()) != null)
        {                
            char[] seps = { ' ' };
            string[] sd = input.Split(seps, StringSplitOptions.RemoveEmptyEntries);

            string[] l = new string[1000];

            for (int i = 0; i < sd.Length; i++)
            {
                l[i] = sd[i];
                textBox4.AppendText(l[i] + "\r\n");

                //The date is 10 characters in length. ex:06/08/2008
                if (l[i].Length == 10)                    
                textBox1.AppendText(l[i]+"\r\n");

                //The time is of 8 characters in length. ex:00:04:09
                if (l[i].Length == 8)
                textBox2.AppendText(l[i] + "\r\n");

                //The phone is of 11 characters in length. ex:9480455302#
                if (l[i].Length == 11)
                textBox3.AppendText(l[i] + "\r\n");                    
            }                
         }
StreamReader rr=File.OpenText(“C:/1.txt”);
字符串输入=null;
而((input=rr.ReadLine())!=null)
{                
char[]seps={''};
string[]sd=input.Split(seps、StringSplitOptions.removeMptyEntries);
字符串[]l=新字符串[1000];
对于(int i=0;i

你能帮我做这个吗

日期中似乎有a/,你可以用它来获取索引,然后返回到行或空格的开头,然后继续前进到空格

伪代码:

获取行中第一个/第二个的位置

索引=位置

startpos,endpos

而索引!=0

而char[索引]!=“”

索引--//执行此操作直到日期开始 (即日期前空格行的开始) //找到索引了吗? startpos=索引

索引=位置 而char[索引]!=“” index++//执行此操作直到到达日期后的空格

//找到索引了吗

endpos = index
日期=子字符串(startpos,endpos-startpos)


p.S.我对正则表达式很差劲…

似乎日期中有一个/,你可以用它来获取索引,然后返回,直到你到达行或空格的开头,然后继续前进,直到你到达空格

伪代码:

获取行中第一个/第二个的位置

索引=位置

startpos,endpos

而索引!=0

而char[索引]!=“”

索引--//执行此操作直到日期开始 (即日期前空格行的开始) //找到索引了吗? startpos=索引

索引=位置 而char[索引]!=“” index++//执行此操作直到到达日期后的空格

//找到索引了吗

endpos = index
日期=子字符串(startpos,endpos-startpos)


p.S.我在正则表达式方面很差劲…

在这种情况下,最好的选择是使用更精确的正则表达式,并且不需要任何形式的格式化。。。 一般正则表达式是“[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}”,您可以根据需要调整它,在匹配中可以找到匹配值,即确切的日期。。
我碰巧看到silverlight内置了一个很好的正则表达式求值器,在这种情况下,最好的选择是使用更精确的正则表达式,并且不需要任何形式的格式化。。。 一般正则表达式是“[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}”,您可以根据需要调整它,在匹配中可以找到匹配值,即确切的日期。。
我碰巧看到silverlight内置了一个很好的正则表达式计算器

我用您提供的文本在console应用程序中尝试了正则表达式。这很有效:

        Regex reg = new Regex(@"\d{4}/\d{2}/\d{2}|\d{2}/\d{2}/\d{4}|\d{2}/\d{2}");

        string str = @"2008/04/06 00:35:35 193111 1008 O 9448050132# 74
           2008/04/06 00:35:35 193116 1009 O 9448050132# 74
           12/15/2008 8:36AM 106 01 090788573 00:01'23' ..06
           10/10/2008 14:32:32 4400 4653 00:00:56 26656 0 0 OG AL# & 0000 0000
           N 124 00 8630 T001045 10/16 05:04 00:01:02 A 34439242360098";

        MatchCollection mc = reg.Matches(str);

        foreach (Match m in mc)
        {
            Console.WriteLine(m.Value);
        }

我想你可以一行一行地阅读,从每一行中获取匹配项,并将它们保存在某个列表或数组中以供以后使用。

我使用你提供的文本在console应用程序中尝试了regex。这可以:

        Regex reg = new Regex(@"\d{4}/\d{2}/\d{2}|\d{2}/\d{2}/\d{4}|\d{2}/\d{2}");

        string str = @"2008/04/06 00:35:35 193111 1008 O 9448050132# 74
           2008/04/06 00:35:35 193116 1009 O 9448050132# 74
           12/15/2008 8:36AM 106 01 090788573 00:01'23' ..06
           10/10/2008 14:32:32 4400 4653 00:00:56 26656 0 0 OG AL# & 0000 0000
           N 124 00 8630 T001045 10/16 05:04 00:01:02 A 34439242360098";

        MatchCollection mc = reg.Matches(str);

        foreach (Match m in mc)
        {
            Console.WriteLine(m.Value);
        }

我认为您可以逐行读取,并从每行中获取匹配项,并将它们保存在某个列表或数组中以供以后使用。

代码中有一些奇怪之处。最值得注意的是,while循环中的以下行:

string[] l = new string[1000];
char[] seps = { ' ' };
这将为while循环中的每一轮创建一个1000个元素的字符串数组。稍后,您将在该数组中仅使用元素
i
,而其他999个元素未使用。从代码的其余部分来看,您也可以简单地使用
sd[i]

此外,我猜测textBox1、textBox2和textBox3不应该包含相同的值;如果一个值进入其中一个,它就不应该进入另一个值(除了似乎收集所有数据的textBox4)。那么,一旦找到正确的textbox,也不需要继续测试该值

最后,while循环中的以下行:

string[] l = new string[1000];
char[] seps = { ' ' };
这将为while循环中的每一轮创建一个相同的字符数组;您可以将该字符数组移到循环之外,然后重新使用相同的数组

至于日期检测,根据您提供的数据,日期是唯一包含/字符的数据,因此您可以测试该数据,而不是长度

您可以尝试以下操作:

StreamReader rr = File.OpenText("C:/1.txt");
string input = null;
char[] seps = { ' ' };
while ((input = rr.ReadLine()) != null)
{    
    string[] sd = input.Split(seps, StringSplitOptions.RemoveEmptyEntries);
    for (int i = 0; i < sd.Length; i++)
    {
        textBox4.AppendText(sd[i] + "\r\n");

        if (sd[i].Contains("/"))
        {
            // The string contains a / character; assume it is a date
            textBox1.AppendText(sd[i] + "\r\n");
        }
        else if (sd[i].Length == 8)
        {
            //The time is of 8 characters in length. ex:00:04:09
            textBox2.AppendText(sd[i] + "\r\n");
        }
        else if (sd[i].Length == 11)
        {
            //The phone is of 11 characters in length. ex:9480455302#
            textBox3.AppendText(sd[i] + "\r\n");
        }
    }                
 }
StreamReader rr=File.OpenText(“C:/1.txt”);
字符串输入=null;
char[]seps={''};
而((input=rr.ReadLine())!=null)
{    
string[]sd=input.Split(seps、StringSplitOptions.removeMptyEntries);
对于(int i=0;i
<