Wednesday, July 29, 2015

A Simple Introduction To Regular Expressions

Scott Hanselman has a saying, once you solve a problem with a Regular Expression, you now have two problems.  Regular expressions can be intimidating due to their syntax.  However, regular expressions can be understood and debugged, as long as you have the right tools.  They can be powerful if used appropriately and correctly.  You can do it.

The Is Numeric Regular Expression

Our first regular expression will detect if the entire string is numbers.

^\d+$

Let's break down each character of the regular expression:

  • ^ This means there must be a match at the beginning of the string
  • \d This means to detect a number
  • + This means to detect 1 or more of the preceding item (in other words we are looking for one or more numbers)
  • $ This means there must be a match at the end of the string

Try it out in an HTML5 compliant browser

Here is the HTML code for the above.  Notice we are using "tel" for the input type.  This will bring up the numeric keyboard for mobile devices.
<input pattern="^\d+$" required="required" title="Please enter a number" type="tel" />

The 5 Digit Zip Code Regular Expression


Let's take what we know from above and extend our knowledge.  We can easily make a five digit zip code detector.

^\d{5}$

In this example we have introduced a new concept with the {5}.  This means we are looking for exactly 5 characters.

Try it out in an HTML5 compliant browser

Here is the HTML code for the above.  Again, we are using "tel" for the input type which will bring up the numeric keyboard for mobile devices.
<input pattern="^\d{5}$" required="required" title="Please enter 5 digit zip code" type="tel" />


The Zip Code with optional Zip+4 Regular Expression


Now we are starting to look like the familiar overwhelming spaghetti of characters that characterizes regular expressions.  When you see something like what is presented below your first reaction is to throw up your hands and say to yourself, "I can't understand this mess."  The best way to think about it is to review it character by character.  If you still don't understand it, tools like Expresso can be invaluable to explaining it to you.

^\d{5}(-?\d{4})?$

In this regular expression we are now adding the concept of groups.  A group is characterized by open and close parenthesis ( ).  We are also adding the concept of the question mark ? character.  The question mark character indicates there can be zero or one instance.  So within the group there can be an optional dash followed by four digits.  The entire group for the four digits is optional because there is a question mark following.  Expresso explains all this:


So this will match

  • 12345
  • 123456789
  • 12345-6789


Try it out in an HTML5 compliant browser

Here is what the HTML Code looks like:
<input pattern="^\d{5}(-?\d{4})?$" required="required" title="Please enter 5 digit zip code or zip+4" type="tel" />

The Currency Regular Expression

In our currency example we want to allow the user to enter in a $ and commas along with numbers and cents.  The expression contains these new concepts:


  • \$ means to escape the $ end of string operator with a normal $.  Any special characters need to be escaped such as ^$([]{}.\
  • [0-9] this is a range operator meaning digits from 0 through 9.  It also works with letters too.  Example [a-z] or [A-Z] or [a-zA-Z] for both lower and upper case.
  • {1,3} is a character range operator meaning we want from 1 to 3 characters
  • * means that any number of characters



^\$? ?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$

This will match

  • $1,240.99
  • $ 1.240.99
  • $10,000
  • 10000.00
Here is what it looks like in Expresso






Try it out in an HTML5 compliant browser
This is what the HTML code looks like:
<input pattern="^\$? ?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$" required="required" title="Please enter currency amount" type="text" />

The US Phone Regular Expression


For a phone in the US we want ten numbers.  The area code and prefix can be separated by common characters.  Notice the set operator.  This is denoted by [-. ].  This means we will match a dash, a period, or a space.

^\(?\d{3}[\)-\.]? ?\d{3}[-. ]?\d{4}$

This will match

  • (999) 999-9999
  • (999)999-9999
  • 999-999-9999
  • 999.999.9999
  • 9999999999


Try it out in an HTML5 compliant browser



Here is the HTML Code:
<input pattern="^\(?\d{3}[\)-\.]? ?\d{3}[-. ]?\d{4}$" required="required" title="Please enter valid phone in the format (999) 999-9999 or 999-999-9999" type="tel" />

Valid URL Regular Expression


We are now going to introduce several different things for a regular expression that validates a URI.  I took the valid characters from this lovely stack overflow post.  .NET supports the concept of named groups.  However JavaScript does not.  See the groups below Protocol, Prefix, Domain and QueryString.  It is also possible to use OR statements inside the regular expression by using the pipe | operator.  In this particular case the or operator is used to work with escape HTML characters such as %20 means a space.

.NET Named Group Example
^(?<Protocol>https?://)(?<Prefix>www\d*.)(?<Domain>([!#$&-;-\[\]_a-z~]|%[0-9a-fA-F]{2})+)(?<QueryString>\??([!#$&-;=-\[\]_a-z~]|%[0-9a-fA-F]{2})+)?$

JavaScript Equivalent
^(https?://)(www\d*.)(([!#$&-;-\[\]_a-z~]|%[0-9a-fA-F]{2})+)(\??([!#$&-;=-\[\]_a-z~]|%[0-9a-fA-F]{2})+)?$

This will match

  • http://www.kellermansoftware.com
  • http://kellermansoftware.com
  • https://www.kellermansoftware.com
  • http://www1.kellermansoftware.com
  • kellermansoftware.com
  • www.kellermansoftware.com
  • http://www.kellermansoftware.com?FirstName=Greg




Try it out in an HTML5 compliant browser

Here is the HTML:
<input pattern="^(https?://)(www\d*.)(([!#$&amp;-;-\[\]_a-z~]|%[0-9a-fA-F]{2})+)(\??([!#$&amp;-;=-\[\]_a-z~]|%[0-9a-fA-F]{2})+)?$" required="required" title="Please enter valid url" type="text" />

Date Regular Expression


Even though HTML5 supports the date input type, it is not supported by most browsers as of yet.  You probably should be using either the JQuery date picker or the bootstrap date picker if you are using bootstrap.  Here is a regular expression for dates that I wrote about before..

When should you use regular expressions?

Client side validation is always a good candidate for regular expressions.  This is whether you are doing a web application, smart client, or phone application.  Regular expressions can be slow when running against megabytes of text, even if compiled beforehand.  If you are dealing with large data sets or large sets of regular expressions you should test the performance vs. plain indexof string parsing.  Sometimes I have split up large text using IndexOf then run a regular expression on the remainder.

Resource for Regular Expressions

Reggie - If you are just starting out, this is a nice self contained HTML page that has a reference built in.
Expresso - This is an incredible free editor.  It is written in .NET so it supports authoring of named groups.  It will run from a flash drive if your PC is locked down.
Regexlib - A library of regular expressions.  Test these the quality is intermittent based on the author.
Regular Expression Cheat Sheet by Dave Child
Regular Expression Tutorial by DZone





Friday, April 10, 2015

Getting Certified in Oracle

Oracle Certification Program
http://education.oracle.com/pls/web_prod-plq-dad/db_pages.getpage?page_id=50

Oracle Database Certification
http://education.oracle.com/pls/web_prod-plq-dad/ou_product_category.getFamilyPage?p_family_id=32&p_mode=Certification

Study Guides
http://www.oracle.com/partners/en/most-popular-resources/certified-specialist-study-guide-322505.html
http://www.oracle.com/partners/en/knowledge-zone/database/oracle-database-11g/db12c-exam-study-guide-2202720.pdf
http://www.amazon.com/Oracle-Database-Fundamentals-Guide-1Z0-061/dp/0071820280/

Videos
https://www.youtube.com/watch?v=T-rCbe1MMG4&list=PLL_LQvNX4xKzVcg_GoRzjeM_veo8CI15A
https://www.youtube.com/watch?v=0NLsJQCvKXY&list=PLL_LQvNX4xKwbz1aJe0RofbT9YeJH9huQ
http://www.pluralsight.com/courses/oracle-database-12c-fundamentals
http://www.pluralsight.com/courses/oracle-database-12c-installation-upgrade
http://www.pluralsight.com/courses/oracle-database-12c-disaster-recovery
http://www.pluralsight.com/courses/oracle-plsql-fundamentals
http://www.pluralsight.com/courses/oracle-plsql-fundamentals-part2
http://www.pluralsight.com/courses/oracle-plsql-transactions-dynamic-sql-debugging

Oracle Quick References
http://docs.oracle.com/cd/E11882_01/server.112/e41085.pdf
http://www.digilife.be/quickreferences/QRC/ORACLE%20Server%208i%20Quick%20Reference%20Card.pdf
http://www.digilife.be/quickreferences/QRC/ORACLE%20Server%20Architecture.pdf
http://www.cheat-sheets.org/saved-copy/oracle_sql_reference.pdf

Friday, February 13, 2015

Using Jasmine with Visual Studio

Jasmine Overview

Jasmine is a JavaScript unit testing framework similar to MS Test or NUnit.  Jasmine requires four references to get it to work:
  • Jasmine.js
  • Jasmine-html.js
  • Jasmine.css
  • boot.js
Let's test the following JavaScript:

function ExtractNumbers(text) {
    if (!text)
        return text;
    
    return text.replace( /\D+/g, '');
}

Here is the test that you would write in Jasmine:

describe("Common tests", function() {

    it("ExtractNumbers method should extract numbers from a string", function () {
        expect(ExtractNumbers("Logan 5, Francis 7")).toBe("57");
    });

});

I have created a JSFiddle to demonstrate:

Using Jasmine with Visual Studio and MS Test


Follow these steps to create a Jasmine project with Visual Studio:
1.  Create a blank web project (not a class library like you would do for MS Test or NUnit)
2.  Create a similar folder structure for the JavaScript files you wish to test.
3.  Add the JavaScript files as links in your project
4.  You will need to manually download Jasmine from GitHub and put the files in the Scripts folder since there is no recent Nuget package.
5.  Under Tools -> Extensions and updates, Visual Studio Gallery.  Search and Install Chutzpah Test Adapter for the Test Explorer which is a JavaScript test runner for Visual Studio.  Also install Chutzpah Test Runner Context Menu Extension.  Chutzpah works with QUnit, Jasmine, and Mocha testing frameworks.
6.  Add a JavaScript file that will be used for the tests.  Add a reference at the top to the JavaScript file you wish to test by simply dragging and dropping the file to the top.  Here is an example:

/// <reference path="../../UI/Scripts/Common.js" />

describe("Common tests", function() {

    it("ExtractNumbers method should extract numbers from a string", function () {
        expect(ExtractNumbers("Logan 5, Francis 7")).toBe("57");
    });

});

Using Jasmine with Resharper

Follow the same steps above for Resharper except Chutzpah is not required for Resharper.  Resharper works out of the box with QUnit and Jasmine to run JavaScript tests.

Wednesday, February 11, 2015

Using QUnit with Visual Studio

QUnit Overview


QUnit is a JavaScript unit testing framework similar to MS Test or NUnit.  In order for QUnit to work, you need a reference to QUnit.js and also a QUnit.css file.

Given the following JavaScript:

function ExtractNumbers(text) {
    if (!text)
        return text;
    
    return text.replace( /\D+/g, '');
}

Here is a corresponding QUnit test:

QUnit.test( "ExtractNumbers", function( assert ) {
  assert.equal( ExtractNumbers("Logan 5, Francis 7"), "57", "Passed!" );
});

I have created a JSFiddle to demonstrate:
http://jsfiddle.net/gfinzer/tmdn2nfj/6/


Using QUnit with Visual Studio and MS Test

Follow these steps to create a QUnit project with Visual Studio:
1.  Create a blank web project (not a class library like you would do for MS Test or NUnit)
2.  Create a similar folder structure for the JavaScript files you wish to test.
3.  Add the JavaScript files as links in your project
4.  Add a NuGet package reference to QUnit
5.  Under Tools -> Extensions and updates, Visual Studio Gallery.  Search and Install Chutzpah Test Adapter for the Test Explorer which is a JavaScript test runner for Visual Studio.  Also install Chutzpah Test Runner Context Menu Extension.  Chutzpah works with QUnit, Jasmine, and Mocha testing frameworks.
6.  Add a JavaScript file that will be used for the tests.  Add a reference at the top to the JavaScript file you wish to test by simply dragging and dropping the file to the top.  Here is an example:

/// <reference path="../../UI/Scripts/Common.js" />

QUnit.test("ExtractNumbers", function (assert) {
    assert.equal(ExtractNumbers("Logan 5, Francis 7"), "57", "Passed!");

});

Using QUnit with Resharper

Follow the same steps above for Resharper except Chutzpah is not required for Resharper.  Resharper works out of the box with QUnit and Jasmine to run JavaScript tests.


Tuesday, January 6, 2015

Developer Conferences for 2015

Here is the list of developer conferences for the Midwest and the major U.S. national conferences.  If you would like your conference listed.  Please contact me and I will add it.

CodeMash V2.0.1.5

Jaunary 6-9, Sandusky, Ohio
Conference Only – Thursday and Friday: $300
Conference + Single Precompiler – Wednesday through Friday: $450
Conference + Double Precompiler – Tuesday through Friday: $600
http://codemash.org/


KalamazooX

March 14, Kalamazoo, Michigan
Price $50
http://kalamazoox.org/

 

CocoaConf

March 27-28, Chicago, IL
Conference Only:  $550
Conference + Workshop:  $700
http://cocoaconf.com

Code PaLOUsa

Pricing:  $50
April 27-30, Louisville, KY, USA
http://www.codepalousa.com/


Visual Studio Live

March 16-20, 2015, Las Vegas, Nevada
June 1-4, 2015, Austin, TX
June 15-18, 2015, San Fransisco, CA
August 10-14, Redmond, WA
September 28-Oct 1, Brooklyn, NY
November 16-20, Orlando, FL
http://vslive.com

Build 2015

April 29-May 1, 2015, San Francisco, CA
Previous Pricing for 2014:  $2095
http://www.buildwindows.com/

StirTrek

Price $75
May 1, 2015, Columbus, Ohio
http://www.stirtrek.com/

Microsoft Ignite

May 4-8, Chicago, IL
Expo Only Pass:  $300
Day Pass:  $500
Full Conference Pass $2,220
http://ignite.microsoft.com

GoTo Conference

Pricing $950 to $2700 depending upon when you register and how many workshop days
May 11-12, Chicago IL
http://gotocon.com/chicago-2015/

 

Dev Intersection

Pricing $1595 to $2942 depending upon when you register and how many pre-conference days
May 18-24, Scottsdale, AZ
http://devintersection.com

 

QCon

June 8-12 New York, NY
Price $1500 to $2300 depending upon how early you register
https://qconnewyork.com/

 

PyOhio

Free
http://www.pyohio.org/

Codestock 2015

Sometime in July, Knoxville, Tennessee
No Pricing Announced Yet:  ~$100
http://www.codestock.org/

Agile 2015

August 3-7, Washington DC
Price $1999
http://agile2015.agilealliance.org/

That Conference

August 10-12, 2015, Wisconsin Dells, WI
No price announced this year.  Last years price was: $349
Tickets go on sale starting May 13, 2015 at 8:10am!
http://www.thatconference.com/

devLink

Sometime in August, Chattanooga, Tennessee
No price announced this year.  Last years price was: $~100 to ~200
http://www.devlink.net/

Midwest Tech Fest (formerly Central Ohio Day of .NET)

Early Fall, Columbus, Ohio
No price announced this year.  Last years price was: $0 to $10
http://mwtechfest.com/
https://www.facebook.com/MidWestTechFest
https://twitter.com/MidwestTechFest

Dev Connections

September 14-17, Las Vegas, NV
Essentials:  $1299
All Access :  $2099
http://www.devconnections.com/

 

CloudDevelop

Sometime in October, Columbus, Ohio
No price announced this year.  Last years price was: $20
http://clouddevelop.org/

Columbus Code Camp

Sometime in October, Columbus, Ohio
Free
http://columbuscodecamp.com/

 

M3 Mobile Conference

Sometime in November, Columbus, Ohio
No price announced this year.  Last years price was: $125 -$250
http://www.m3conf.com

 

St Louis Day of .NET

Sometime in November, Columbus, Ohio
No price announced this year.  Last years price was: $300
http://stldodn.com


Dog Food Conference

Sometime in November, Columbus, OH
No price announced yet.  Last year was $50
http://www.dogfoodcon.com

Free PluralSite Subscription with MSDN

Microsoft has announced a free subscription to PluralSite with MSDN.  It includes 30 courses.  After signing up, you can upgrade for a full subscription for 15% off.

Use this link:
https://msdn.microsoft.com/subscriptions/pluralsight30

Here are the courses included:

Applied Windows Azure
Architecting Applications for the Real World in .NET
ASP.NET MVC 5 Fundamentals
Asynchronous C# 5.0
Building a Site with Bootstrap, AngularJS, ASP.NET, EF and Azure
Building an Intelligent System: From Embedded to Cloud
Building Web Apps & Services with Entity Framework and Web API
C# Generics
Defensive Coding in C#
Enterprise WPF with XAML and C# from Scratch
Getting Started With Release Management for Visual Studio 2013
Hack-proofing Your ASP.NET Web Applications
IntelliTrace
Introduction to Visual Studio 2013 - Part 1
Introduction to Visual Studio 2013 - Part 2
Lessons from Real World .NET Code Reviews
Mastering Visual Studio 2013
Microsoft Fakes Fundamentals
Microsoft Test Manager 2013
Migrating Internet Applications to Azure
Mobilize Your ASP.NET Web Forms
Plan, Create, and Deploy to Azure With Visual Studio Online
Real World Scrum With Team Foundation Server 2013
Security and Encryption in SQL Server 2012 and 2014
SharePoint 2013 Fundamentals
Team Foundation Server 2013 New Features
VB.NET Fundamentals
Visual Studio Data Tools for Developers
Visual Studio LightSwitch 2012 and 2013
Windows Azure Infrastructure as a Service Essentials