Validating Royal Mail shipping with vb.net and regular expressions

I

idletask

I needed to use a regular expression to validate the shipping code that
was/is scanned by my application.

I use a windows (winform) application in Vb.net. I did it by creating
a function to handle the validation, and return true or false depending
on the value it received (which is just input from a scanner).


Make sure to import system.text.regularexpressions

Private Function ValidWaybill(ByRef strWaybill As String) As Boolean

If Regex.IsMatch(strWaybill, "^([A-Z]{2})([
]{1})([0-9]{4})([ ]{1})([0-9]{4})([ ]{1})([0-9]{1})([A-Z]{2})$") = True
Then
ValidWaybill = True
Else
ValidWaybill = False
End If



End Function

This works for me, at least it seems to.
The format of the value I need to validate is like this:

"AA 0000 0000 0AA"

So I made an expression that looks for two capital letters, followed by
a space. Next is two sets of 4 digit numbers, followed by spaces.
Last is one number, followed by two capital letters.

In my production code, the expression is stored in an XML file, and
this function reads it when it runs. This way, if I made a mistake, I
can fix it without re-compiling.

Hope this is usefull for someone!

Ciao.

Idletask
 
K

Ken Tucker [MVP]

Hi,

RegexLib.com has plenty of examples of regular expressions. I
believe this the pattern you are looking for.

http://www.regexlib.com/REDetails.aspx?regexp_id=583

Ken
------------------
I needed to use a regular expression to validate the shipping code that
was/is scanned by my application.

I use a windows (winform) application in Vb.net. I did it by creating
a function to handle the validation, and return true or false depending
on the value it received (which is just input from a scanner).


Make sure to import system.text.regularexpressions

Private Function ValidWaybill(ByRef strWaybill As String) As Boolean

If Regex.IsMatch(strWaybill, "^([A-Z]{2})([
]{1})([0-9]{4})([ ]{1})([0-9]{4})([ ]{1})([0-9]{1})([A-Z]{2})$") = True
Then
ValidWaybill = True
Else
ValidWaybill = False
End If



End Function

This works for me, at least it seems to.
The format of the value I need to validate is like this:

"AA 0000 0000 0AA"

So I made an expression that looks for two capital letters, followed by
a space. Next is two sets of 4 digit numbers, followed by spaces.
Last is one number, followed by two capital letters.

In my production code, the expression is stored in an XML file, and
this function reads it when it runs. This way, if I made a mistake, I
can fix it without re-compiling.

Hope this is usefull for someone!

Ciao.

Idletask
 
I

idletask

Hi-
thanks for that link. I didn't know about that website. I think I
have the pattern I need though. I am not an expert with regular
expressions, but that pattern looks a little different than what I
need. I will compare this to what I came up and and see what works
better.

Thanks again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top