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:

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:

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