Untitled
By: a guest | Aug 25th, 2008 | Syntax:
C# | Size: 3.00 KB | Hits: 194 | Expires: Never
using System;
using System.Text.RegularExpressions;
using System.Data;
using System.Collections;
namespace retest
{
class Program
{
static void Main(string[] args)
{
System.IO.
StreamReader sr
= new System.IO.
StreamReader("sample.txt");
String fileContent = sr.ReadToEnd();
sr.Close();
String cp = @"\s+(-?[\d.]+)";
Regex re
= new Regex
(@"^(\S+)"+cp
+cp
+cp
+cp
+cp
+cp
+cp
+cp
+".*", RegexOptions.
Multiline);
MatchCollection mc = re.Matches(fileContent);
DataTable table
= new DataTable
();
DataColumn CodeColumn
= table.
Columns.
Add("Code",
typeof(string));
table.
Columns.
Add("Open",
typeof(float));
table.
Columns.
Add("High",
typeof(float));
table.
Columns.
Add("Low",
typeof(float));
table.
Columns.
Add("Close",
typeof(float));
table.
Columns.
Add("Change",
typeof(float));
table.
Columns.
Add("Trade",
typeof(float));
table.
Columns.
Add("Volume",
typeof(float));
table.
Columns.
Add("Value",
typeof(float));
table.
Columns.
Add("Line",
typeof(string));
foreach (Match m in mc)
{
table.
Rows.
Add(new object[] { m.
Groups[1
].
Value, m.
Groups[2
].
Value, m.
Groups[3
].
Value, m.
Groups[4
].
Value, m.
Groups[5
].
Value, m.
Groups[6
].
Value, m.
Groups[7
].
Value, m.
Groups[8
].
Value, m.
Groups[9
].
Value, m.
Groups[0
].
Value});
}
table.AcceptChanges();
DataRow[] allRows;
allRows = table.Select();
DataRow[] foundRows;
ArrayList list
= new ArrayList
();
for (int ii = 0; ii < allRows.Length; ii++)
{
if (!list.Contains(allRows[ii]["Code"]))
{
list.Add(allRows[ii]["Code"]);
string expression = "Code = '" + allRows[ii]["Code"] + "'";
foundRows = table.Select(expression);
if (foundRows.Length == 2)
{
Console.WriteLine(expression + ":");
Console.WriteLine("row 0:");
Console.WriteLine(foundRows[0]["Code"] + "," + foundRows[0]["Open"] + "," + foundRows[0]["High"] + "," + foundRows[0]["Low"] + "," + foundRows[0]["Close"] + "," + foundRows[0]["Change"] + "," + foundRows[0]["Trade"] + "," + foundRows[0]["Volume"] + "," + foundRows[0]["Value"] + "," + foundRows[0]["Line"]);
Console.WriteLine("row 1:");
Console.WriteLine(foundRows[1]["Code"] + "," + foundRows[1]["Open"] + "," + foundRows[1]["High"] + "," + foundRows[1]["Low"] + "," + foundRows[1]["Close"] + "," + foundRows[1]["Change"] + "," + foundRows[1]["Trade"] + "," + foundRows[1]["Volume"] + "," + foundRows[1]["Value"] + "," + foundRows[1]["Line"]);
Console.WriteLine("===================");
}
}
}
}
}
}