Link to home
Create AccountLog in
Avatar of Dodsworth
Dodsworth

asked on

Generating Postcodes

Found this link re: Regular Expressions for UK Postcodes.

http://www.qwghlm.co.uk/blog/2005/09/08/a-better-postcode-regexp/

I need to generate random (yet valid) UK postcodes to test my app.

Ideas please.
Avatar of Stephen Manderson
Stephen Manderson
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi there,

Please take a look at the following questions.

This will generate every possible combination of uk postcodes.

in format

A00 0AA

You just need to define the additional loops for the other formats etc..

ie

A9 9AA
A9A 9AA
AA9 9AA
AA99 9AA
AA9A 9AA

    Private Function PostcodeList() As List(Of String)
        Dim postcodes As New List(Of String)
        Dim s1, s2, s3, s4, s5 As String
        Dim ascA As Integer = Asc("A")
        Dim ascZ As Integer = Asc("Z")
        Dim asc0 As Integer = Asc("0")
        Dim asc9 As Integer = Asc("9")
        For char1 As Integer = ascA To ascZ
            s1 = Chr(char1)
            For char2 As Integer = asc0 To asc9
                s2 = s1 & Chr(char2)
                For char3 As Integer = asc0 To asc9
                    s3 = s2 & Chr(char3) & " "
                    For char4 As Integer = asc0 To asc9
                        s4 = s3 & Chr(char4)
                        For char5 As Integer = ascA To ascZ
                            s5 = s4 & Chr(char5)
                            For char6 As Integer = ascA To ascZ
                                postcodes.Add(s5 & Chr(char6))
                            Next
                        Next
                    Next
                Next
            Next
        Next
        Return postcodes
    End Function

And also here is a function that will check if a postcode is a valid one.

    Private Function IsPostcode(ByRef PossiblePostcode As String) As Boolean

        Dim CheckPostcode As String = PossiblePostcode.Replace(" ", "")

    Dim Format1 As New Regex("[A-Z]\d\d[A-Z][A-Z]") 'A99AA
    Dim Format2 As New Regex("[A-Z]\d\d\d[A-Z][A-Z]") 'A999AA
    Dim Format3 As New Regex("[A-Z]\d[A-Z]\d[A-Z][A-Z]") 'A9A9AA
    Dim Format4 As New Regex("[A-Z][A-Z]\d\d[A-Z][A-Z]") 'AA99AA
    Dim Format5 As New Regex("[A-Z][A-Z]\d\d\d[A-Z][A-Z]") 'AA999AA
    Dim Format6 As New Regex("[A-Z][A-Z]\d[A-Z]\d[A-Z][A-Z]") 'AA0A0AA

        If Format1.IsMatch(CheckPostcode) Then
            Return True
        ElseIf Format2.IsMatch(CheckPostcode) Then
            Return True
        ElseIf Format3.IsMatch(CheckPostcode) Then
            Return True
        ElseIf Format4.IsMatch(CheckPostcode) Then
            Return True
        ElseIf Format5.IsMatch(CheckPostcode) Then
            Return True
        ElseIf Format6.IsMatch(CheckPostcode) Then
            Return True
        Else
            Return False
        End If

    End Function
Avatar of Dodsworth
Dodsworth

ASKER

Wonderful...
but I need Random postcodes.

Just one at the call of a function.
Just select a random index from the returned list of string and you have a random postcode
Isn't the list going to be huge ?

I'd rather have a function ; )
Millions upon millions of possible outcomes :P

300 odd I ended up with

The main problem is if you want to test a postcode you would be best with valid ones to start with.

What are you returning from the test?
I need a random (ish) spectrum of postcodes to present on a map.

About 10 to 20,000 would be good!
Oi Experts !

Work your magic ! (please)
Hi there,

Heres a way to get random poscode values.

The fact is they will be random and most likely invalid as far as mapping to an address however to get 20,000 valid postcodes you would need a PAF file.

Regards
Steve
    Private Function RandomPostcode() As String
 
        Dim RndPost As String = ""
        Dim Chars As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        Dim Digits As String = "0123456789"
        Dim Rnd As New Random
 
        RndPost &= Chars.Substring(Rnd.Next(0, 25), 1)
        RndPost &= Digits.Substring(Rnd.Next(0, 9), 1)
        RndPost &= " "
        RndPost &= Digits.Substring(Rnd.Next(0, 9), 1)
        RndPost &= Chars.Substring(Rnd.Next(0, 25), 1)
        RndPost &= Chars.Substring(Rnd.Next(0, 25), 1)
 
        Return RndPost
 
    End Function

Open in new window

Yes I have something similar but how can I validate them as true postcodes ?
You need to validate against the royal mail postcode address file which isnt free and cost about £1700 last time I purchased it.

Regards
Steve
I was wondering if there was some way of posting to google maps and detecting if the postcode was found ?
You would need to validate against an online system, but companies such as royal mail will limit to at most 15 searches a day.

And Google ?
nope afraid not... well not easily that is, you need some where to load the postcodes that will return a specific format so you can determine if it is real or not. If you tell me the postcode location you are after i could give you 10 or 20 valid postcodes
Well I've worked out the code to do this but not sure if I should post it as I don't think validating against a map provider is legal is it ?
dunno mate :) best not post the specifics just to be safe
Avatar of ddrudik
If you want to generate strings that conform to the regex shown in your question:
Module Module1
    Sub Main()
        For x = 1 To 20
            Console.WriteLine(RandomPostCode())
        Next
    End Sub
    Public Function RandomPostCode() As String
        Dim part1 As String = RandomChar("ABCDEFGHIJKLMNOPRSTUWYZ", 1, 1)
        Dim part2 As String = RandomChar("ABCDEFGHKLMNOPQRSTUVWXY0123456789", 1, 1)
        Dim part3 As String = RandomChar("ABCDEFGHJKSTUW0123456789", 0, 1)
        Dim part4 As String = RandomChar("ABEHMNPRVWXY0123456789", 0, 1)
        Dim part5 As String = RandomChar(" ", 1, 2)
        Dim part6 As String = RandomChar("0123456789", 1, 1)
        Dim part7 As String = RandomChar("ABDEFGHJLNOPQRSTUWXYZ", 2, 2)
        Return part1 & part2 & part3 & part4 & part5 & part6 & part7
    End Function
    Public Function RandomChar(ByVal str As String, ByVal min As Integer, ByVal max As Integer)
        Static randGen As New Random
        Dim randIter As Integer = randGen.Next(min, max + 1)
        Dim retStr As String = ""
        Dim i As Integer = 0
        If randIter = 0 Then
            Return ""
        Else
            While i < randIter
                Static randGen2 As New Random
                retStr = retStr & str.Chars(randGen2.Next(0, str.Length))
                i = i + 1
            End While
            Return retStr
        End If
    End Function
End Module

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ddrudik
ddrudik
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Thanks for the question and the points.