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. Verifying Addresses: Confirm the validity of a what3words address using the what3words API.
  3. Cross-Language Code Samples: Implement these techniques in Python, JavaScript, Java, Swift, Objective-C, PHP, .NET, Ruby, Dart, Rust and GoLang.

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” (“is_possible_3wa” for Python & Rust API wrapper) can be 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.

2Verifying what3words addresses

Overview

Our API wrapper RegEx function “isValid3wa” (“is_valid_3wa” for our Python & Rust API wrapper) 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.

3Regex 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
4Contact 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