Month: June 2012

C# .NET & RegEx

Create a new Windows Application project in Visual Studio .NET. Open the Form Designer and add the following controls:

1. Button

a. Name: btnTestRegEx

b. Text: Test RegEx

2. TextBox

a. Name: tbxRegEx

b. Dock: Left

c. MultiLine: True

d. Text: Site Name:(?’SiteName’.*)Site Address:(?’SiteAddress’.*)Site Contact:(?’SiteContact’.*)

3. TextBox

a. Name: tbxInput

b. Dock: Left

c. MultiLine: True

d. Text:

Site Name: www.NunoSolutions.com

Site Address:

100 Main Street

MyCity, NJ 012345-1234

Site Contact: Nuno F. Pereira

4. TextBox

a. Name: tbxSiteName

b. MultiLine: False

c. Text: Site Name

5. TextBox

a. Name: tbxSiteAddress

b. MultiLine: False

c. Text: Site Address

6. TextBox

a. Name: tbxSiteContact

b. MultiLine: False

c. Text: Site Contact

You want to make sure your form design view looks something like the following:

Next, double click on the Test RegEx button to hook the Click event. You should now be in the code view for your form and VS.Net should present you with an event declaration similar to the following:

private void btnTestRegEx_Click(object sender, EventArgs e)

{

}

Let’s add the following code to the button Click event. Copy and Paste the following within the scope of btnTestRegEx_Click event. Your code should now look like the following :

private void btnTestRegEx_Click(object sender, EventArgs e)

{

string SiteName = “”, SiteAddress = “”, SiteContact = “”;

Match regExMatch = Regex.Match(textBox1.Text,

myRegExpression, RegexOptions.Singleline);

if (regExMatch.Success)

{

SiteName = regExMatch.Groups[“SiteName”].Value.Trim().ToUpper();

SiteAddress = regExMatch.Groups[“SiteAddress”].Value.Trim().ToUpper();

SiteContact = regExMatch.Groups[“SiteContact”].Value.Trim().ToUpper();

}

tbxSiteName.Text = SiteName;

tbxSiteAddress.Text = SiteAddress;

tbxSiteContact.Text = SiteContact;

}

When you run your application and click the Test RegEx button, the values should properly get parsed and set to their respective textboxes.

 

Here’s how it should look:

Force SSL in IIS7 (via URL Rewrite Module)

In order for this to work you’ll need to install the URL Rewrite module on your machine first.

Just know that after installing the module on your machine might force you to restart IIS and/or the machine itself.

Add the following to your web.config:

<system.webServer>

 

  <rewrite>

    <rules>

      <clear />

      <rule name=Redirect to https stopProcessing=true>

        <match url=(.*) />

        <conditions>

          <add input={HTTPS} pattern=off ignoreCase=true />

        </conditions>

        <action type=Redirect url=https://{HTTP_HOST}{REQUEST_URI} redirectType=Permanent />

      </rule>

    </rules>

  </rewrite>

 

</system.webServer>