I am trying to parse the following string coming as an input from a text file:
Eurex App: X_RISK Trader: TTADM XXX 100 UL: TTADM Workstation: xx.x.x.xx Server: xx.xx.xx.xx
LIFFE App: X_RISK Trader: TTADM XXX 100 UL: TTADM Workstation: xx.x.X.xx Server: xx.xx.xx.xx
I want to be able to extract the following values and put them into an array of datarows.
Eurex X_RISK TTADM XXX 100 TTADM xx.x.x.xx xx.xx.xx.xx
Eurex X_RISK TTADM XXX 100 TTADM xx.x.x.xx xx.xx.xx.xx
Eurex X_RISK TTADM XXX 100 TTADM xx.x.x.xx xx.xx.xx.xx
I got it to a point where I am almost there, but still not quite I am having some difficulties how to get rid of the text ending with the double columns ":" and keep only the value I need so I can place them in my data table.
Please look at my code below, any help and suggestions are highly appreciated.
public DataSet ConvertNew(string sLogFile)
{
if (!System.IO.File.Exists(sLogFile))
return null;//fail file not found
stream = new StreamReader(sLogFile, true);
//Create a new data set
DataSet dsFile = new DataSet("File");
DataTable dtTable = dsFile.Tables.Add("Data");
dtTable.Columns.Add("Exchange", typeof(string));
dtTable.Columns.Add("Application", typeof(string));
dtTable.Columns.Add("Trader", typeof(string));
dtTable.Columns.Add("UL", typeof(string));
dtTable.Columns.Add("Workstation", typeof(string));
dtTable.Columns.Add("Server", typeof(string));
Regex r = new Regex(",(?=([^\"]*\"[^\"]*\")*(?![^\"]*\"))");
Regex regExpr = new Regex("\\s\\S");
Regex patternExpr = new Regex("(App:|Trader:|UL:|Workstation:|Server:|:)");
// Get the first line of data from the input (log) file.
val = NextLine(stream);
//Strip out each field and insert in DataTable
int sStart;//used as a placeholder for our current location
int nCount;//used to determine which field / column we are currently extracting
string sTemp;//used to temporarily hold the field pulled out by RegEx in order to trim the quotation marks from the ends of the fiel (if any)
//Data row object used to set values
DataRow drRow;
while (val != "")//val is empty when the input stream has been read to the end
{
sStart = 0;
nCount = 0;
//create a new row
drRow = dtTable.NewRow();
//Iterate through all the matches (match=field in current row)
foreach (Match m in patternExpr.Matches(val))
{
//Retrieve the field based on the results of the match
sTemp = val.Substring(sStart, m.Index - sStart);
drRow[nCount] = sTemp;
//keep one step ahead in the matching game..
sStart = m.Index + 1;
//keep track of which field is next
nCount++;
}
dtTable.Rows.Add(drRow);
val = NextLine(stream);
}
return dsFile;
}
*****This function I use to simply read line by line from the text file:
private static String NextLine(StreamReader stream)
{
int stemp = stream.Read();
String sReturn = "";
while (stemp != -1 && stemp != '\n')/
{
sReturn += (char)stemp;
stemp = stream.Read();
}
return sReturn;
}