Regex or alternative to search zipcodes? | XM Community
Skip to main content
Greetings all, I am looking for a way to create a filter that will allow me to filter out everyone that does not live within 50 miles of our HQ. We do all our surveys on site and have determined that this is the maximum radius where we can reliably get folks to come in. I have a list of 667 zipcodes that make up all the zipcodes within 50 miles of our location but now I need to use that as a filter.



I chatted with support and they said that this is not a built in function but that I could either make a "saved filter" with 667 individual lines (not my preference 🙂 ) or I could try hitting the forums for help.



So, does can anyone think of a way (possibly Regex?) to create a filter that basically says "show if zipcode [zipcode is an embedded data field for us on our directory members] is equal to 90405 OR 90404 OR...." for the list of 667 zipcodes?



I tried using ^[9][0-3][0-9][0-9][0-9]$ as the min and max range of zipcodes is 90001-93599 but the problem is that not *every* zipcode in that range is within 50 miles of our location. So I end up with about 10% - 15% more results than I should.



Also, I have been fiddling around with it and I have determined what zipcodes appear in that range that should not and it's about 250. So That gets me down to writing a saved filter with 251 lines (which is better than 667) but that's still a lot more than I'd like to be doing.



Thanks!
Better solution is to use authenticator. As you already vhave list of valid zipcodes. So make your list. On using authenticator if respondent enter the zip that is not valid, they will get error message and you can terminate too.

https://www.qualtrics.com/support/survey-platform/survey-module/survey-flow/advanced-elements/authenticator/authenticator-overview/
@ironspider - It sounds like you want to base your survey logic off whether the person's zip code is on your list or not. Regex or an authenticator isn't appropriate for that because those would prevent someone from entering their zip code.



You could copy and paste your list of zip codes into an array in an addOnPageSubmit function JavaScript function that checks to see if the entered zip code is in the array, then set an embedded data variable to 1 if it is in the array and 0 if it isn't.
Thanks for the suggestions but for me I think the issue is not tagging these people or locking them out at signup--because I don't want to stop people from signing up and I don't need to capture this only going forward but I need to be able to search the 25,000 people I already have in my Directory. So I actually figured out a way to do this in one Regex Match string and just saved that as a "Saved Filter". It's a little involved but it did work! And I got exactly the number of filtered contacts I should have (I confirmed it by exporting the entire directory into Excel and checking there). So here's how I did it.



To create a reusable filter based on zipcode radius using Regex Matching:



1. Get your list of Zipcodes. I did this using searchbug.com and freemaptools.com. They both gave the same results so I ended up with 667 zipcodes.

2. Paste these into column B in an Excel spreadsheet. One zipcode per row.

3. Now use Find and Replace to replace every numerical digit with a square brackets version of itself. For example, do a find for all 9's in the document and replace them with [9]. You need to do this because of the way Regex matches things. You will need to do this procedure 10 times. So all 0 become [0], all 1 become [1], all 2 become [2], etc. When you are done every digit in every zipcode should have square brackets around it. So 90001 becomes [9][0][0][0][1]

4. In column A put a single ^ character (this tells regular expressions to "look only at the start of an entry for the following") for all rows.

5. In column C put a single $ character (this tells regular expressions to search for "the end of the line at this position") for all row.

6. In column D to the right put a single | character (this "pipe" or "vertical bar" acts like a logical "or" to a regex entry)

7. In column E write the following formula to combine all these elements: =CONCAT(A1,B1,C1,D1)

8. Highlight everything in Column E and copy and "paste values" into column F. At this point you should have an entire column of zipcodes that now look like this: ^[9][0][0][0][1]$|

9. Now copy that entire column and go to a new worksheet and in cell A1 do a "paste transpose." This will paste all your rows into the top column (so in my case I now had 1 row and 667 columns instead of 1 column and 667 rows.

10. Copy that row and paste it into a text editing program that allows you to do find and replace on special characters. I use notepad++ because it's free and powerful.

11. At this point we are close but this process gives us tabs between the transformed zipcodes. We need to remove them. Hit Ctrl+F to bring up the find/replace box and click on the Replace tab.

12. In the "search mode" box select "Extended". This allows you to find/replace things like tabs.

13. In the "Find what" box type \\t

14. Leave the "Replace with" field blank

15. Hit "Replace all." This will simply get rid of all the tab characters.

16. Go to the end of the string of characters and remove the last vertical pipe | character (not sure if this step is necessary but better safe than sorry).

17. You now have a long string of the transformed zipcodes.

18. Save that file for future use (for example if you wanted to add some more zipcodes to it later.

19. In Qualtrics. Any time you need to do a filter now you can simply paste that into a "Matches Regex" box. For me, the filter was Embedded Data > Zipcode > Matches Regex > ^[9][0][0][0][1]$|^[9][0][0][0][2]$|^[9][0][0][0][3]$|^[9][0][0][0][4]$| etc.

20. Make sure you put a check in the "Add to my saved filters" checkbox so you can reuse the filter!



I know that's a lot of steps but in reality it only took about 15 minutes to do once I had it figured out. The filter itself took about 6 minutes to run on a Directory of 25,000 contacts that returned around 9800 people. If you use the filter with other search conditions (because what I do is use this saved filter and then add to it with other conditions) it takes much less time (adding another filter condition for me took the search time down to 3 minutes).



I hope this is a feature that can be added as default functionality but in the meantime, this should work.
@ironspider - Got it. I originally thought you were asking about a survey instead of the contacts directory.
@TomG



I'm looking to do something similar to what you suggested using a list to validate a text entry. Would you be able to provide some example code for how I would go about doing that?



Thank You!

Leave a Reply