Permalink
Browse files

Part 4: Regular Expressions

Last time we left off with a GUI for our keyword checker program. What
would be the next logical building block to add now that we have our
checker with a nice preview pane?

Well the preview pane is not too handy if it does not show the part that
we are looking for, so let us improve that today. We will use regular
expressions to achieve this. In short, a regular expression acts as a
text search, but instead of using a specific keyword to search for, it
is generally more effective with a pattern that finds a set of text
results that match this pattern. So our first piece of code is to add a
reference to System.Text.RegularExpressions at the top of our form code.

http://www.edvella.com/Post/23/Beginners_C_Part_4_Regular_Expressions
  • Loading branch information...
edvella
edvella committed May 3, 2014
1 parent 32aca54 commit 6182218dfef14ba6361783789a2f5461fc2e1a76
Showing with 43 additions and 4 deletions.
  1. +4 −0 README.md
  2. +8 −2 keywordChecker/Form1.Designer.cs
  3. +31 −2 keywordChecker/Form1.cs
View
@@ -0,0 +1,4 @@
Keyword Checker (Beginner's C# Tutorial Series)
===============================================
This project is the accompanying code for the beginner's C# tutorial at [edvella.com](<http://www.edvella.com/Post/20/Beginners_Scraping_With_C_Part_1>).

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -6,6 +6,7 @@
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
@@ -32,16 +33,44 @@ private void btnCheck_Click(object sender, EventArgs e)
StringBuilder sb = new StringBuilder();
if (keywordLocation >= 0)
{
var pageIds = Regex.Matches(pageContent, @"id=""\s*?\S*?""");
string matchedId = closestId(keywordLocation, pageIds);
string idTag = matchedId.Substring(4, matchedId.Length - 5);
brwPreview.Navigate(url + "#" + idTag);
sb.AppendFormat("{0} are talking about {1} today.", url, keywords);
sb.Append("\n\nSnippet:\n" + pageContent.Substring(keywordLocation, 100));
brwPreview.Navigate(url);
sb.AppendFormat("\n\nClosest id: {0}", idTag);
}
else
{
sb.Append("Keyword not found!");
}
lblResult.Text = sb.ToString();
}
private string closestId(int keywordLocation, MatchCollection matchingIds)
{
int? closestId = null;
string closestIdName = null;
foreach (Match id in matchingIds)
{
if (closestId != null)
{
int idDistance = Math.Abs(id.Index - keywordLocation);
if (idDistance < closestId.Value)
{
closestId = idDistance;
closestIdName = id.Value;
}
}
else
{
closestId = Math.Abs(id.Index - keywordLocation);
closestIdName = id.Value;
}
}
return closestIdName;
}
}
}

0 comments on commit 6182218

Please sign in to comment.