Reputation: 145
I have a string that looks like this:
<br /><br />\n\n<p><font size=\"4\" face=\"Courier New\"> TSX Symbol Changes -December
17th - December 21st</font><br>
What i need to do is pull out TSX Symbol Changes -December 17th - December 21st. I've read on various other posts that Regex.IsMatch works for this situation but the problem I have is that December 17th - 21st will change every week (i.e. when i run my code next week the name will change to TSX Symbol Changes - December 24th - December 28th). So is there anyway I can look for just TSX Symbol Changes and once thats found retrieve the dates after it as well?
Upvotes: 0
Views: 113
Reputation: 460268
Use Html Agility Pack
if you need to parse html.
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html); // this is your string
string wordToFind = "TSX Symbol Changes -";
var fontTSX = doc.DocumentNode.Elements("font")
.FirstOrDefault(f => f.InnerText.Contains(wordToFind));
if (fontTSX != null)
{
string innerText = fontTSX.InnerText.Trim();
innerText = innerText.Substring(innerText.IndexOf(wordToFind) + wordToFind.Length);
String[] words = innerText.Split();
String monthName = words.First();
var monthInfo = CultureInfo.InvariantCulture.DateTimeFormat.MonthNames
.Select((mn, i) => new{ MonthName = mn, Value = i+1 })
.FirstOrDefault(x => x.MonthName.Equals(monthName, StringComparison.OrdinalIgnoreCase));
if (monthInfo != null)
{
int month = monthInfo.Value;
int day = int.MinValue;
// now extract your range
IEnumerable<int> days = words
.Where(w => w.Length >= 2 && int.TryParse(w.Substring(0, 2), out day))
.Select(w => day)
.Take(2);
if(days.Count() == 2)
{
DateTime startDate = new DateTime(DateTime.Now.Year, month, days.ElementAt(0));
DateTime endDate = new DateTime(DateTime.Now.Year, month, days.ElementAt(1));
}
}
Upvotes: 0
Reputation: 1369
You can try the code bleow.
var str1 = "<br /><br />\n\n<p><font size=\"4\" face=\"Courier New\"> TSX Symbol Changes -December 17th - December 21st</font><br>";
var str2 = str1.Substring(str1.IndexOf("TSX Symbol Changes")).Replace("</font><br>","");
Upvotes: 2