What I need is to test every line grouped per date and count number of a certain pattern found in it. In this example find and count "D;" per date
Sample
My expected output to be displayed into textbox
01 - 3
02 - 0
3 - 4
Right now, What I have started is just to read the textfile display just the number of line in the textfile.
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();
}
Just a simple idea on how to start this will be a big help. Thanks
You can use Regex
string test = "D; sjhjsd D; sdsdjks D;";
MatchCollection collection = Regex.Matches(test, @"[D;]+");
var x= collection.Count;
result :3
This edit show how you can implement above Regex
for counting every readed line
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();
}
Use regular expressions:
MatchCollection mc = Regex.Matches(lineread, "D;");
int count = mc.Count;
Whole function would be like this:
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);
}
Try with this:
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;
Output:
01 - 3
02 - 0
03 - 4
You can change the pattern and the dates if you want. Example:
2016-12-07 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";
Output:
07 - 0
11 - 3
25 - 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.