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.
In this example we will just look at those codes that begin with PO and GU
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
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
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
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
So now let's add in the PO patterns.
We can tell regex to look for postal codes the start with 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 . Let's add this in
Lastly the final character in the PO postal codes can be a range of 0-7. So lets now add that in
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/