There are some real inconsistencies here. The Regex you provided ^([a-zA-Z]\d[a-zA-z]( )?\d[a-zA-Z]\d)$
matches what was stated by Scott regarding the correct Canadian format. However, the examples you provided do not follow the format B1C C1B or B1CC1B
.
To add insult to injury, the Regex you provided works with the proper Canadian format. So there isn't any real reason to change it. I mean, I would change it to this ^([a-zA-Z]\d[a-zA-Z]\s?\d[a-zA-Z]\d)$
so that the single space isn't grouped, but that's just me.
However, as far as using it, it could be used in C# like this:
var matches = Regex.Match(inputString, @"^([a-zA-Z]\d[a-zA-Z]( )?\d[a-zA-Z]\d)$");
if (!matches.Success) {
// do something because it didn't match
}
and now that it's been tagged with VB.NET:
Dim matches = Regex.Match(inputString, "^([a-zA-Z]\d[a-zA-Z]( )?\d[a-zA-Z]\d)$")
If Not matches.Success Then
' do something because it didn't match
End If