Answers.Body: <p>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!!! </p>
  
  <p>In case it's helpful for anyone else, here's some example regex's for postcode searches:</p>
  
  <p>Eg1: - find all strings that begin with S25: <code>^S25.*</code> </p>
  
  <p>Meaning: <code>^</code> = start at the beginning of the string, <code>.*</code> = followed by any character, repeated any number of times</p>
  
  <p>Eg2, Find all postcodes beginning with a string between NE15 to NE47:</p>
  
  <pre><code>^NE(1[5-9]|[23][0-9]|4[0-7]).*
  </code></pre>
  
  <p>Meaning:
  <code>^</code> = 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:</p>
  
  <p><code>1[5-9]</code> = the number 1 followed by any number from 5 to 9 (ie 15-19), OR,
  <code>[23][0-9]</code> = The number 2 or 3 followed by any number from 0 to 9 (ie 20 - 39), OR,
  <code>4[0-7]</code> = The number 4 followed by any number from  0 to 7 (ie 40-47),
  <code>.*</code> = any character, repeated zero or any number of times. </p>
  
  <p>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: <a href="http://gamon.webfactional.com/regexnumericrangegenerator/" rel="nofollow noreferrer">http://gamon.webfactional.com/regexnumericrangegenerator/</a> </p>
  
  <p>Eg3: Place many postcodes searches on one line of regex in search builder:</p>
  
  <pre><code>(^S25.*)|(^TS.*)|(^HG.*)|(^HU.*)|(^DN.*)|(^WF.*)|(^HX.*)|(^HD.*)|(^BD.*)
  </code></pre>
  
  <p>This finds all postcodes beginning with S25, or TS, or HG, or HU, etc etc.</p>
  
  <p>Hopefully that's helpful. </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-02T19:15:18.840Z
Answers.Text: Nice answer. thanks for taking the time to pass on your learning in such a clear way Andy
Answers.UserDisplayName: petednz - fuzion
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-02T15:25:26.860Z
Answers.LastActivityDate: 2017-08-02T15:25:26.860Z
Answers.UserDisplayName: Andyg8
Answers.Body: <p>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:</p>
  
  <pre><code>^(AB|DD|DG|EH|FK|G|HS|IV|KA|KW|KY|ML|PA|PH|TD|ZE)[0-9].*
  </code></pre>
  
  <ul>
  <li>I'm using a list of Postcode Sectors (the initial letters) retrieved here: <a href="https://en.wikipedia.org/wiki/Category:Postcode_areas_covering_Scotland" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Category:Postcode_areas_covering_Scotland</a>;</li>
  <li>Following the list with <strong>[0-9].*</strong> ensures that postcodes within <strong>G</strong> (Glasgow sector) are found but <strong>GL</strong> (Gloucester sector, for example) are not;</li>
  <li>Although using <strong>\d</strong> to find a digit is accepted by Search Builder it fails to find anything so has to be expanded to <strong>[0-9]</strong> instead. (I've not fully investigated this unexpected feature.)</li>
  </ul>
  
  <p>This contribution is late to the party here but I hope it will be useful to someone.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-05T13:42:25.523Z
Answers.LastActivityDate: 2018-09-05T13:42:25.523Z
Answers.UserDisplayName: David Hepper
Body: <p>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. </p>
  
  <p>See below for an example of the postcodes from one of our regions.</p>
  
  <p>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. </p>
  
  <p>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?</p>
  
  <p>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.</p>
  
  <p>Each of our regions have a list of postcodes like follows:</p>
  
  <p>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)</p>
  
  <p>Thanks for any thoughts!</p>
  
Tags: <search><search-builder>
Title: Best way to search through multiple postcode regions
ThreadText: # Best way to search through multiple postcode regions
  
  (Asked by: username_0 on 2017-07-11)
  
  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!
  
  **Tags:** search, search-builder
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2017-08-02. Score: 1 | Accepted answer
  
  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:
  ^NE(1[5-9]|[23][0-9]|4[0-7]).*
  
  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:
  (^S25.*)|(^TS.*)|(^HG.*)|(^HU.*)|(^DN.*)|(^WF.*)|(^HX.*)|(^HD.*)|(^BD.*)
  
  This finds all postcodes beginning with S25, or TS, or HG, or HU, etc etc.
  Hopefully that's helpful.
  
  > **Comments:**
  > **username_1**: Nice answer. thanks for taking the time to pass on your learning in such a clear way Andy  
  
  ---
  
  ### Answer by username_2 on 2018-09-05. Score: 1
  
  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:
  ^(AB|DD|DG|EH|FK|G|HS|IV|KA|KW|KY|ML|PA|PH|TD|ZE)[0-9].*
  
  
  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.
  
  
  ---
  
  