Welcome to EMC Consulting Blogs Sign in | Join | Help

SSIS Junkie

Once upon a time this blog was a hive of activity. Now however its pretty lifeless as you can probably tell so if are pining for more of the same you can find me over at http://sqlblog.com/blogs/jamie_thomson. I look forward to seeing you there!

SSIS Nugget: The script component and regular expressions

Another quick nugget here to demonstrate Regular Expressions. For those that don't know, regular expressions are a nifty way of matching incoming textual values to a required format. Probably best that I don't try and explain it any more. Read on...

One of the classic demo uses of regular expressions is to examine a UK postcode to see if it indeed a valid postcode and I'm going to use that same demo situation herein. For those that don't know, the format of a UK postcode is 1 or 2 letters, followed by 1 or 2 digits, optionally followed by a space, and finally a digit and 2 letters. The following are all valid UK postcodes:

  • TW118PW
  • LI3 7YT
  • W1 9WE
  • WE23 0OQ

In my demo I've used a script component as a source to generate 7 postcodes as test data. The regular expression is implemented inside another script component. And finally, I have passed the results to a Trash component. The Trash Component is produced by Konesans. You will need to download and install it which is very very easy to do.

Here's the data-flow containing these 3 components:

20050704_RegEx_nugget1.JPG

And here is the all important code within the "Check postcode" component:

Imports System.Text.RegularExpressions

...

...

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
    Dim r As Regex
    Dim m As Match
    Dim pattern As String = String.Empty

    pattern =
"^[A-Z][A-Z]?[0-9][0-9]?[A-Z]? ?[0-9][A-Z][A-Z]$"
    r = New
Regex(pattern, RegexOptions.Compiled)
    m = r.Match(Row.PostCode)
   
    If
m.Success Then
        Row.ValidPostCode = True
    Else
        Row.ValidPostCode = False
    End If
End Sub

 

And when you run it....here's what ya get. I've added a data viewer so that you can see the results:

20050704_RegEx_nugget2.JPG

As you can see, the regular expression has successfully identified the valid and invalid postcodes based on the criteria I specified.

You can download this demo package from here.

In case you want to have a further play around with regular expressions you may want to check out regular-expressions.info which is a useful reference site.

-Jamie

 

Comments

 

Jamie Thomson's Blog said:

I hate commuting. There's no two ways of saying it, I despise it. Every day I face the 70 minute grind...
July 5, 2005 09:58
 

Jamie Thomson's Blog said:

I hate commuting. There's no two ways of saying it, I despise it. Every day I face the 70 minute grind...
July 5, 2005 20:32
 

Jamie Thomson's Blog said:

I hate commuting. There's no two ways of saying it, I despise it. Every day I face the 70 minute grind...
July 25, 2005 16:10
 

Jamie Thomson - Life, the universe and SSIS! said:

I hate commuting. There's no two ways of saying it, I despise it. Every day I face the 70 minute grind...
July 27, 2005 16:04
 

Jamie Thomson - Life, the universe and SSIS! said:

A long time ago in a galaxy far far away I posted a blog entry (http://blogs.conchango.com/jamiethomson/archive/2005/07/04/1754.aspx)...
January 3, 2006 15:29
 

Jamie Thomson - Life, the universe and SSIS! said:

A long time ago in a galaxy far far away I posted a blog entry (http://blogs.conchango.com/jamiethomson/archive/2005/07/04/1754.aspx)...
January 4, 2006 14:19
 

Jamie Thomson - Life, the universe and SSIS! said:

A long time ago in a galaxy far far away I posted a blog entry (http://blogs.conchango.com/jamiethomson/archive/2005/07/04/1754.aspx)...
January 4, 2006 14:21
 

Jamie Thomson - Life, the universe and SSIS! said:

A long time ago in a galaxy far far away I posted a blog entry (http://blogs.conchango.com/jamiethomson/archive/2005/07/04/1754.aspx)...
January 4, 2006 14:22
 

Nice blog said:

Very useful blog. Thank you.
February 22, 2006 20:25
 

SSIS Junkie said:

About a month ago I wrote this very simple blog post explaining how one might go about stripping off

December 19, 2006 09:20
 

gautam beri said:

hey can some one tell me that how can i access the variables defined in the package from a script, using it as a source

February 4, 2008 15:30
New Comments to this post are disabled

This Blog

Syndication

Powered by Community Server (Personal Edition), by Telligent Systems