Advanced Search only lets you search for a range of postal codes. What do you do if you want to search for lots of different postcodes at the same time? For example you might want to search for all the postal codes that are in Austin, Texas or for the postal codes in the UK county of Hampshire.

Search Builder comes to the rescue with a handy search operator called RLIKE.

RLIKE accepts searches using regex pattern searches. In this example we are going to find the postcodes for Hampshire using 4 simple regex 'rules'.

First let's see what postcodes we need. I found them here.

http://www.postcode-info.co.uk/hampshire-postcodes-370.html

In this example we will just look at those codes that begin with PO and GU

GU10

GU11

GU12

GU13

GU14

GU17

GU26

GU30

GU31

GU32

GU33

GU34

GU35

GU46

PO10

PO11

PO12

PO13

PO14

PO15

PO16

PO17

PO7

PO8

PO9

Now crank up CiviCRM, go to Search Builder and start a search on Contact > Postal code >RLIKE

Now lets start seeing if there are patterns, one character at a time. Lets start with GU.

Well, there we are already! We can see that there are a bunch of codes that start with GU. Let's regex that then.

### Rule 1 "Starts with"

There a special character regex uses to say "Starts with" It's this ^ (Shift-6 on a UK keyboard)

So lets use it to find postal codes starting with GU. Type into the search field

^GU

Click search.

Works!

OK what comes after GU? The next character can be a 1,2,3or 4. That's a range. 1 - 4.

### Rule 2. Character ranges

Regex has an expression for ranges. It looks like this [1-4].

So now let's add that to our expression

^GU[1-4]

Works!

Now lets look at the next character.

We can see that it is also a range of characters between 0 and 7. So let's add that into our regex and test it

^GU[1-4][0-7]

Works!

So that deals with the GU postal codes. What about the PO ones. Let's tell regex to look for those too.

### Rule 3 OR

We add in a 'pipe' symbol | (shift-\ on a UK keyboard). That means 'OR' in regex

^GU[1-4][0-7]|

So now let's add in the PO patterns.

We can tell regex to look for postal codes the start with PO

^GU[1-4][0-7]|^PO

If we then look for the third character, we can see for PO it can only be 1,7,8 or 9.

### Rule 4 Character sets

In regex we express this as [1789]. Let's add this in

^GU[1-4][0-7]|^PO[1789]

Lastly the final character in the PO postal codes can be a range of 0-7. So lets now add that in

^GU[1-4][0-7]|^PO[1789][0-7]

You can now add in all the other postcodes and end up with a complete regex expression for Hampshire postal codes. Do the search and save it as a Smart Group called Hampshire!

Tip: Build your regex expressions in a text editor just in case your browser crashes and you lose your work.

You can learn a lot more about regex here http://www.regular-expressions.info/