All tutorials

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

intermediate

Detecting and validating what3words addresses can be crucial for various applications, such as logistics, emergency response, and even personal navigation. This tutorial aims to guide you through the process of using Regular Expressions (RegEx) to identify and verify what3words addresses in text strings.

What You’ll Learn

  1. Matching what3words address format: Use RegEx to identify if a text format exactly matches a what3words address.
  2. Finding Addresses in Text: Search for embedded what3words addresses within larger text blocks.
  3. Verifying Addresses: Confirm the validity of a what3words address using the what3words API.
  4. Cross-Language Code Samples: Implement these techniques in Python, JavaScript, Java, Swift, Objective-C, PHP, .NET, Ruby and Dart.

Benefits

  • Quick Validation: Rapidly check if a string is a what3words address.
  • Efficient Search: Locate addresses within text effortlessly.
  • Error Prevention: Flag potential addresses for further validation.
  • Flexible Integration: Easily add what3words detection to your applications.

By the end of this tutorial, you’ll be equipped to enhance your applications with precise and reliable what3words address handling using RegEx.

1Matching what3words address format

Overview

Our API wrapper RegEx function “isPossible3wa” can be used used to detect if a text string (like “filled.count.soap“) in the format of a what3words address without having to ask the API. This functionality checks if a given string could be a what3words address. It returns true if it could be, otherwise false.

Benefits

  • Quick Validation: Rapidly determine if a string resembles a what3words address.
  • Error Prevention: Helps avoid errors by flagging potential what3words addresses for further validation.
import { AutosuggestClient } from "@what3words/api";

// Initialise the What3Words API with your API key
const API_KEY = 'YOUR_API_KEY';
const client = AutosuggestClient.init(API_KEY);

// Example what3words addresses
const addresses = ["filled.count.soap", "not a 3wa", "not.3wa address"];

// Function to check if the address is a possible what3words address
const checkPossible3wa = (address) => {
  const isPossible = client.isPossible3wa(address);
  console.log(`Is '${address}' a possible what3words address? ${isPossible}`);
};

// Check each address
addresses.forEach(checkPossible3wa);
Copied

Expected Output

  • isPossible3wa(“filled.count.soap”) returns true
  • isPossible3wa(“not a 3wa”) returns false
  • isPossible3wa(“not.3wa address”) returns false

Steps

  1. Initialise the API with your What3Words API key. [Note: Not applicable for Java wrapper]
  2. Check if each address is a possible what3words address using isPossible3wa.
  3. Print the result for each address.

Dependencies

Ensure you have the necessary dependencies installed for the what3words API Wrapper that you have chosen. For more information, refer to our what3words API Wrappers.

2Find what3words address within text

Overview

Our API wrapper RegEx function “findPossible3wa” can be used to detect a what3words address within a block of text, useful for finding a what3words address in fields like Delivery Notes. For example, it can locate a what3words address in a note like “Leave at my front door ///filled.count.soap”. The function will match if there is a what3words address within the text. If no possible addresses are found, it returns an empty list.

Benefits

  • Efficient Search: Quickly locate what3words addresses embedded within text.
  • Flexible Integration: Easily incorporated what3words address detection into your applications.

Notes: The expressions are designed to work across languages but do not work for Vietnamese (VI) due to spaces within words.

import { AutosuggestClient } from "@what3words/api";

const API_KEY = 'YOUR_API_KEY';
const client = AutosuggestClient.init(API_KEY);

// Example texts
const texts = [
    "Please leave by my porch at filled.count.soap",
    "Please leave by my porch at filled.count.soap or deed.tulip.judge",
    "Please leave by my porch at"
];

// Function to find possible what3words addresses in text
const findPossible3wa = (text) => {
    return client.findPossible3wa(text);
};

// Check each text for possible what3words addresses
texts.forEach((text) => {
    const possibleAddresses = findPossible3wa(text);
    console.log(`Possible what3words addresses in '${text}':`, possibleAddresses);
});
Copied

Expected Output

  • findPossible3wa(“Please leave by my porch at filled.count.soap”) returns ['filled.count.soap']
  • findPossible3wa(“Please leave by my porch at filled.count.soap or deed.tulip.judge”) returns ['filled.count.soap', 'deed.tulip.judge']
  • findPossible3wa(“Please leave by my porch at”) returns []

Steps

  1. Initialise the API with your What3Words API key. [Note: Not applicable for Java wrapper]
  2. Define example texts containing possible what3words addresses.
  3. Use the findPossible3wa function to detect what3words addresses in the text.
  4. Print the possible what3words addresses found in each text.

Dependencies

Ensure you have the necessary dependencies installed for the what3words API Wrapper that you have chosen. For more information, refer to our what3words API Wrappers.

3Verifying what3words addresses

Overview

Our API wrapper RegEx function “isValid3wa” can be used to determine if a string is a valid what3words address by checking it against the what3words regex filter and verifying it with the what3words API.

Benefits

  • Reliable Validation: Ensure the accuracy of what3words addresses by confirming their existence.
  • Integration with What3Words API: Seamlessly integrate address verification with the what3words API.
import { AutosuggestClient } from "@what3words/api";

const API_KEY = 'YOUR_API_KEY';
const client = AutosuggestClient.init(API_KEY);

// Example addresses
const addresses = [
    "filled.count.soap",
    "filled.count.",
    "coding.is.cool"
];

// Function to check if the address is valid
const checkIsValid3wa = async (address) => {
    try {
        const isValid = await client.isValid3wa(address);
        console.log(`Is '${address}' a valid what3words address? ${isValid}`);
    } catch (error) {
        console.error(`Error validating address '${address}':`, error);
    }
};

// Check each address for validity
addresses.forEach(checkIsValid3wa);
Copied

Expected Outputs

  • isValid3wa(“filled.count.soap”) returns True
  • isValid3wa(“filled.count.”) returns False
  • isValid3wa(“coding.is.cool”) returns False

Steps

  1. Initialise the API with your What3Words API key.
  2. Define example addresses to validate.
  3. Use the isValid3wa function to check if each address is valid.
  4. Print the validation results for each address.

Dependencies

Ensure you have the necessary dependencies installed for the what3words API Wrapper that you have chosen. For more information, refer to our what3words API Wrappers.

4Regex code samples for exact match of what3words

Overview

If the API Wrapper function isPossible3wa does not meet your needs, you can use RegEx to detect what3words addresses. Below are code samples in multiple languages that define a text string, pass it through the RegEx, and print whether the text contains a what3words address. This method offers a flexible way to identify what3words addresses, such as “filled.count.soap”.

Benefits

  • Language Agnostic: Works across various programming languages.
  • Customisable: Tailor the RegEx pattern to meet specific needs.
  • Simple Integration: Easily incorporated into existing projects.
  • Efficient: Quickly scan text for valid what3words addresses.

Notes:

  • For Python: You might need to explicitly set UTF-8 encoding with reload(sys).setdefaultencoding("utf8") and # -*- coding: utf-8 -*- at the top of your script. Also, import re
const text  = "index.home.raft";
var regex = /^\/{0,}(?:[^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?/";:£§º©®\s]+[.。。・・︒។։။۔።।][^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?/";:£§º©®\s]+[.。。・・︒។։။۔።।][^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?/";:£§º©®\s]+|[^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?/";:£§º©®\s]+([\u0020\u00A0][^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?/";:£§º©®\s]+){1,3}[.。。・・︒។։။۔።।][^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?/";:£§º©®\s]+([\u0020\u00A0][^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?/";:£§º©®\s]+){1,3}[.。。・・︒។։။۔።।][^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?/";:£§º©®\s]+([\u0020\u00A0][^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?/";:£§º©®\s]+){1,3})$/;

if (regex.test(text))
  console.log(text + " is the format of a three word address");
else
  console.log(text + " is NOT the format of a three word address");
}
Copied
5Regex code samples for finding what3words within text

Overview

If the above API Wrapper function findPossible3wa does not meet your requirements, you can use RegEx to detect a what3words address within a block of text. This can be useful for finding a what3words address within fields like Delivery Notes, for example: Leave at my front door ///filled.count.soap. The RegEx will match if there is a what3words address anywhere in the text.

Benefits

  • Efficient Search: Quickly locate what3words addresses within larger text blocks.
  • Language Agnostic: Works across various programming languages.
  • Customizable: Tailor the RegEx pattern to meet specific needs.
  • Simple Integration: Easily incorporate into existing projects.

Notes:

  • The expressions are designed to work across languages but do not work for Vietnamese (VI) due to spaces within words.
  • For Python: You might need to explicitly set UTF-8 encoding with reload(sys).setdefaultencoding("utf8") and # -*- coding: utf-8 -*- at the top of your script. Also, import re
const text  = "The new office is located at ///filled.count.soap, the old office is index.home.raft, and ///searching.contiguous.text is in the Bay of Bengal.";

const regex = /[^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?/";:£§º©®\s]{1,}[.。。・・︒។։။۔።।][^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?/";:£§º©®\s]{1,}[.。。・・︒។։။۔።।][^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?/";:£§º©®\s]{1,}/ig;

const matches = text.match(regex);
console.log(matches);
Copied
6Contact Support

Have any questions?

We’re here to support you! If you have any questions while using our what3words Regex expressions, get in touch with us by writing us an email at support@what3words.com.

Related tutorials