Reputation: 994
I am currently trying to read in from a file and put the data into the right spot in the data. However I am finding that parsing a string in C# is a lot more difficult than c++. An example from the file that I am reading in is as follows:
CGI HOLDING CORP THK 2.15 0.01 0.47 -64.17 6.25 1.92 23.78
into the following data type
public class StockInfo
{
public string Company { get; set; }
public string TickerSymbol { get; set; }
public decimal CurrentPrice { get; set; }
public decimal PriceChange { get; set; }
public decimal PercentChange { get; set; }
public decimal YTDChange { get; set; }
public decimal FiftyTwoWeekHigh { get; set; }
public decimal FiftyTwoWeekLow { get; set; }
public decimal PE_Ratio { get; set; }
}
So Company would = "CGI HOLDING CORP"
Ticker Symbol = "THK"
Current price = 2.15 etc...
However I can't figure out how to parse the data correctly. I have tried using a regular expression but the Name can have different amount of words in it and need to sperate it from the ticker.
Ex: CHICAGO MERCANTILE EX HD CME 301.13 23.53 8.48 31.67 315.00 132.57 35.73
any ideas that can point me in the right direction would be appreciated
Upvotes: 0
Views: 87
Reputation: 9150
If you wanna go for a regex, you can use:
(.+)(\w{3})((?:\s\-?\d{1,3}\.\d{2}){7})
This matches and groups both your examples. The company name will have a trailing whitespace, as well as the "number group" will have a leading whitespace, but this shouldn't be a problem to eliminate this in your code.
Explanation, from right to left:
((?:\s\-?\d{1,3}\.\d{2}){7})
Matches seven times the (non-capturing) group (?:\s\-?\d{1,3}\.\d{2})
while this in turn matches on: a whitespace (\s
) followed by an optional dash (\-?
) followed by one to three numbers (\d{1,3}
) followed by a dot (\.
) followed by two numbers (\d{2}
). So this matches the seven numbers at the end of your input.
(\w{3})
Matches the three-character 'ticker symbol'.
(.+)
Matches all the rest: the company name in your example.
Upvotes: 1
Reputation: 419
I have created an simple Program for you, it may be not the best way, but I think it will bring you to your destination.
string input = "CGI HOLDING CORP THK 2.15 0.01 0.47 -64.17 6.25 1.92 23.78";
List<string> inputSplit = input.Split(' ').ToList();
PE_Ratio = Convert.ToDecimal(inputSplit[inputSplit.Count-1]);
FiftyTwoWeekLow = Convert.ToDecimal(inputSplit[inputSplit.Count - 2]);
FiftyTwoWeekHigh = Convert.ToDecimal(inputSplit[inputSplit.Count - 3]);
YTDChange = Convert.ToDecimal(inputSplit[inputSplit.Count - 4]);
PercentChange = Convert.ToDecimal(inputSplit[inputSplit.Count - 5]);
PriceChange = Convert.ToDecimal(inputSplit[inputSplit.Count - 6]);
CurrentPrice = Convert.ToDecimal(inputSplit[inputSplit.Count - 7]);
TickerSymbol = inputSplit[inputSplit.Count - 8];
for (int i = 0; i < inputSplit.Count - 8; i++)
{
Company = Company + (inputSplit[i] + " ");
}
Company = Company.Trim();
Upvotes: 1