Tutorials | what3words
All tutorials

Detecting if text is in the format of a 3 word address using RegEx

easy

Regex can be used to detect if a text string (like “filled.count.soap”) looks like a 3 word address, without having to ask the API.

We provide two regular expressions: one for an exact match, used for determining if a given text sample is exactly in the form of a 3 word address, and the other is for searching for 3 word addresses in sentences or paragraphs. The difference is the begin and end characters in the RegEx.

Regular expressions are provided for JavaScript, Java, Python, Swift, Objective-C and PHP.

1

RegEx

These are the RegEx for detecting a full 3 word address. For example, “filled.count.soap”. The expressions are designed to work across all languages

var regex = /^\/{0,}[^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?/";:£§º©®\s]{1,}[・.。][^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?/";:£§º©®\s]{1,}[・.。][^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?/";:£§º©®\s]{1,}$/i;
Copied
2

Code Sample

Now we add the RegEx to a code sample that defines the text, passes through the RegEx and then prints whether the text is a 3 word address.

Note for Python: Depending on the languages you are using you might explicitly set utf-8 encoding by calling reload(sys).setdefaultencoding("utf8") at the begining of your script, and placing # -*- coding: utf-8 -*- at the top of your file. Also, don’t forget to import re.

var text  = "index.home.raft";
var regex =  /^\/{0,}[^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?/";:£§º©®\s]{1,}[・.。][^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?/";:£§º©®\s]{1,}[・.。][^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?/";:£§º©®\s]{1,}$/;

if (regex.test(text))
  print(text + " is the format of a three word address");
else
  print(text + " is NOT a three word address");
Copied

Related tutorials

Back to top