I'm in the UK where the postcode regions are - well not exactly designed for the digital age shall we say? I need to group everyone in our database into 7 regions that fit our organisation's needs but the list of postcodes per region is very diverse.

See below for an example of the postcodes from one of our regions.

I'm about to create smart groups via search builder for postcode regions like below, but I know it's going to have 20+ lines in the search builder.

So, I wondered before I start whether there is a better way of doing this? I don't want complicated smart group criteria slowing down our server, since we already have quite a few. Or would these queries not really be an issue?

I'm on Drupal 7.56 and Civi 4.6.24, and about to upgrade to 4.7. I could use Drupal Rules, Views or possibly Search Sorts if they're likely to yield better results.

Each of our regions have a list of postcodes like follows:

Postcodes beginning with: S 25, S 35, S 36, S 60 – 66, S 70 -75, LN 7, DL, YO, LS, DH, SR, TS, HG, HU, DN, WF, HX, HD, BD, NE 1-13, NE 15 – 47, NE 61 – 64, NE 99 (There’s no NE 14)

Thanks for any thoughts!

2 Answers 2


In the end I used Search Builder with regular expressions on the post code field. It was quite efficient once I'd learnt the syntax for regex's!!!

In case it's helpful for anyone else, here's some example regex's for postcode searches:

Eg1: - find all strings that begin with S25: ^S25.*

Meaning: ^ = start at the beginning of the string, .* = followed by any character, repeated any number of times

Eg2, Find all postcodes beginning with a string between NE15 to NE47:


Meaning: ^ = start at the beginning of the string, and find all strings that begin with NE. From there it finds all strings containing any number from 15-47:

1[5-9] = the number 1 followed by any number from 5 to 9 (ie 15-19), OR, [23][0-9] = The number 2 or 3 followed by any number from 0 to 9 (ie 20 - 39), OR, 4[0-7] = The number 4 followed by any number from 0 to 7 (ie 40-47), .* = any character, repeated zero or any number of times.

Numeric ranges have no meaning inside text string searches like Regex, so they have to be constructed as above. I found this online tool to generate them automatically: http://gamon.webfactional.com/regexnumericrangegenerator/

Eg3: Place many postcodes searches on one line of regex in search builder:


This finds all postcodes beginning with S25, or TS, or HG, or HU, etc etc.

Hopefully that's helpful.

  • Nice answer. thanks for taking the time to pass on your learning in such a clear way Andy
    – petednz - fuzion
    Aug 2, 2017 at 19:15

Yes, I agree that RegEx is a good way to solve this. Here's my Search Builder RegEx to select all contacts with postcodes in Scotland, for example:

  • I'm using a list of Postcode Sectors (the initial letters) retrieved here: https://en.wikipedia.org/wiki/Category:Postcode_areas_covering_Scotland;
  • Following the list with [0-9].* ensures that postcodes within G (Glasgow sector) are found but GL (Gloucester sector, for example) are not;
  • Although using \d to find a digit is accepted by Search Builder it fails to find anything so has to be expanded to [0-9] instead. (I've not fully investigated this unexpected feature.)

This contribution is late to the party here but I hope it will be useful to someone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.