2017-01-27 21 views
1

私が必要とするのは、日付にグループ化されたすべての行とその中にある特定のパターンのカウント数をテストすることです。この例では、「D;」を見つけてカウントします。日付テキストファイルを読み込み、ASP.netを使用してデータを操作するC#

今サンプル

enter image description here

My expected output to be displayed into textbox 

01 - 3 

02 - 0 

3 - 4 

ごとに、私が始めたことは、単にテキストファイルの表示にテキストファイルの行数だけを読むことです。

protected void btnRead_Click(object sender, EventArgs e) 
    { 
     string text = String.Empty; 
     int i = 0; 
     using (StreamReader stRead = new StreamReader(FileUpload1.PostedFile.InputStream)) 
     { 

      //to write textfile content   
      while (!stRead.EndOfStream) 
      { 
       //ListBox1.Items.Add(stRead.ReadLine()); 

       text += stRead.ReadLine() + Environment.NewLine; 


       i++; 
      } 
     } 

     TextBox1.Text = text; 
     //Label1.Text = i.ToString(); 
     Label1.Text = this.TextBox1.Text.Split(new Char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries).Length.ToString(); 
     //populateListBox(); 


    } 

これを開始する方法についての簡単なアイデアは大きな助けになります。おかげ

答えて

2

を使用できRegex

string test = "D; sjhjsd D; sdsdjks D;"; 
MatchCollection collection = Regex.Matches(test, @"[D;]+"); 
var x= collection.Count; 

結果:3

あなたは

protected void btnRead_Click(object sender, EventArgs e) 
{ 
    string text = String.Empty; 
    int i = 0; 
    int countedChars; 
    using (StreamReader stRead = new StreamReader(FileUpload1.PostedFile.InputStream)) 
    { 

     //to write textfile content   
     while (!stRead.EndOfStream) 
     { 
      var readedLine = stRead.ReadLine() 
      //ListBox1.Items.Add(readedLine); 

      if (!string.IsNullOrWhiteSpace(readedLine)) 
      { 
       MatchCollection collection = Regex.Matches(readedLine, @"D;"); 
       countedChars = collection.Count; 
       text += readedLine.Substring(0, readedLine.IndexOf(' ')) +" - "+countedChars + Environment.NewLine; 
      } 

      i++; 
     } 
    } 

    TextBox1.Text = text; 
    //Label1.Text = i.ToString(); 
    Label1.Text = this.TextBox1.Text.Split(new Char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries).Length.ToString(); 
    //populateListBox(); 


} 
+0

ありがとうございますゲイン..はこれを試してみます:) – rickyProgrammer

+0

私は試しましたが、この行に "値はnullではありません"というエラーがありましたMatchCollection collection = Regex.Matches(stRead.ReadLine()、@ "[D;] +"); – rickyProgrammer

+1

あなたが '!string.IsNullOrWhiteSpace(someText)'を使用できるアップデートを確認してください –

1

使用正規表現をすべてのreadedラインをカウントするためRegex上に実装することができますどのようにこの編集ショー:

MatchCollection mc = Regex.Matches(lineread, "D;"); 
int count = mc.Count; 

全体の機能は次のようになります:

string[] lines = System.IO.File.ReadAllLines("file.txt"); 
string result = ""; 
for(int i=0;i<lines.Length;i++) 
{ 
    MatchCollection mc = Regex.Matches(lines[i], "D;"); 
    result+= string.Format("{0} - {1}\r\n", i+1, mc.Count); 
} 
+0

lineread?それのためのオプションはありません – rickyProgrammer

+0

私は読まれる行を意味しました。私の編集した答えを確認してください –

+0

先生、私の現在のコードにコードを挿入する場所を知ることができます。ありがとう – rickyProgrammer

1

はこれで試してみてください:

var pattern = "D"; 
while (!stRead.EndOfStream) 
{ 
    line+= stRead.ReadLine(); 

    if (!string.IsNullOrEmpty(line)) 
    { 
     var matches = Regex.Matches(line, @"^\d{4}-\d{2}-(\d{2,3})\sproductkey=([\w;]*)"); 

     if (matches.Count > 0) 
     { 
      var day = matches[0].Groups[1].Value; 
      var productKey = matches[0].Groups[2].Value; 
      var count = Regex.Matches(productKey, pattern).Count; 

      text += string.Format("{0} - {1}{2}", day, count, Environment.NewLine); 
     } 
    } 
} 
TextBox1.Text = text; 

出力:
01から3
02から0
03から4

することができます必要に応じてパターンと日付を変更してください。例:

2016年12月7日のProductKey = D、D、D; 0; 0
2016年12月11日のProductKey = Y; Y; ​​Y; ​​0; 0
2016年12月25日のProductKey = D; D; D; D; 0

var pattern = "Y"; 

出力:
07から0
11から3
25から0

+0

こんにちは!これはありがたいですが、正しい出力が得られていません – rickyProgrammer

+0

テスト用の入力を教えてください。 –

+0

実際には、テキストファイルは質問に表示されている例と非常によく似ています。それは、単に「プロダクトキー」が続かないグループ日付です。それは他の日付で異なる可能性があります。カウントするパターンは「D;」です – rickyProgrammer

関連する問題