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

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.

1Regex code samples for exact match of what3words

Overview

The code samples below demonstrate how to use a regular expression to identify a what3words address (e.g. filled.count.soap) within a text string. This method is flexible and works across multiple programming languages.

It can now also be used to detect what3words addresses embedded within free text, such as in delivery notes during checkout, customer comments, or emails, making it ideal for real-world use cases.

Benefits

  • Language Agnostic: Works across various programming languages
  • Flexible: Can be adapted to match multiple use cases
  • Lightweight: Simple to integrate and quick to run
  • Regex-based: Works offline, no network call required
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
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Example {
    public static void main(String[] args) {
        final String regex = "/*(?:(?:\\p{L}\\p{M}*)+[.。。・・︒។։။۔።।](?:\\p{L}\\p{M}*)+[.。。・・︒។։။۔።।](?:\\p{L}\\p{M}*)+|(?:\\p{L}\\p{M}*)+([\u0020\u00A0](?:\\p{L}\\p{M}*)+){1,3}[.。。・・︒។։။۔።।](?:\\p{L}\\p{M}*)+([\u0020\u00A0](?:\\p{L}\\p{M}*)+){1,3}[.。。・・︒។։။۔።।](?:\\p{L}\\p{M}*)+([\u0020\u00A0](?:\\p{L}\\p{M}*)+){1,3})";
        final String string = "filled.count.soap";
        
        final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
        final Matcher matcher = pattern.matcher(string);
        
        
		if (matcher.find())
  			System.out.println(text + " is the format of a three word address");
		else
  			System.out.println(text + " is NOT the format of a three word address");
    	}
}
Copied
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

text = "index.home.raft"
regex = u"/*(?:[^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?/\";:£§º©®\s]{1,}[.。。・・︒។։။۔።।][^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?/\";:£§º©®\s]{1,}[.。。・・︒។։။۔።।][^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?/\";:£§º©®\s]{1,}|'<,.>?/\";:£§º©®\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 (re.search(regex, text, flags=re.UNICODE)):
    print(text + " is the format of a three word address")
else:
    print(text + " is NOT the format of a three word address")

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Copied
let text         = "index.home.raft"
let regex_string = "/*(?:\\w+[.。。・・︒។։။۔።।]\\w+[.。。・・︒។։။۔።।]\\w+|\\w+([\u{20}\u{A0}]\\w+){1,3}[.。。・・︒។։။۔።।]\\w+([\u{20}\u{A0}]\\w+){1,3}[.。。・・︒។։။۔።।]\\w+([\u{20}\u{A0}]\\w+){1,3})"

let regex = try! NSRegularExpression(pattern:regex_string, options: [])
let count = regex.numberOfMatches(in: text, options: [], range: NSRange(text.startIndex..<text.endIndex, in:text))
if (count > 0) {
  print (text + " is the format of a three word address")
}
else {
  print (text + " is NOT the format of a three word address")
}
Copied
NSString *text         = @"index.home.raft";
NSString *regex = @"/*(?:\\w+[.。。・・︒។։။۔።।]\\w+[.。。・・︒។։။۔።।]\\w+|\\w+([ \u00A0]\\w+){1,3}[.。。・・︒។։။۔።।]\\w+([ \u00A0]\\w+){1,3}[.。。・・︒។։။۔።।]\\w+([ \u00A0]\\w+){1,3})";

NSError *error = NULL;

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regex_string options:NSRegularExpressionCaseInsensitive error:&error];

NSUInteger match_count = [regex numberOfMatchesInString:text options:0 range:NSMakeRange(0, [text length])];

if (match_count > 0)
  NSLog(@"%@ is the format of a three word address.", text);
else
  NSLog(@"%@ is NOT the format of a three word address.", text);
Copied
$text  = "index.home.raft";
$regex = "/\/*(\/*(?:\p{L}\p{M}*){1,}[.。。・・︒។։။۔።।](?:\p{L}\p{M}*){1,}[.。。・・︒។։။۔።।](?:\p{L}\p{M}*){1,}|\/*((?:\p{L}\p{M}*){1,}([\u{0020}\u{00A0}](?:\p{L}\p{M}*){1,}){1,3})[.。。・・︒។։။۔።।]((?:\p{L}\p{M}*){1,}([\u{0020}\u{00A0}](?:\p{L}\p{M}*){1,}){1,3})[.。。・・︒។։။۔።।]((?:\p{L}\p{M}*){1,}([\u{0020}\u{00A0}](?:\p{L}\p{M}*){1,}){1,3}))/u";

if (preg_match($regex, $text))
  print("$text  is the format of a three word address\n");
else
  print("$text is NOT the format of a three word address\n");
Copied
var text = "index.home.raft";
var regex = new Regex("/*(?:(?:\\p{L}\\p{M}*)+[.。。・・︒។։။۔።।](?:\\p{L}\\p{M}*)+[.。。・・︒។։။۔።।](?:\\p{L}\\p{M}*)+|(?:\\p{L}\\p{M}*)+([\u0020\u00A0](?:\\p{L}\\p{M}*)+){1,3}[.。。・・︒។։။۔።।](?:\\p{L}\\p{M}*)+([\u0020\u00A0](?:\\p{L}\\p{M}*)+){1,3}[.。。・・︒។։။۔።।](?:\\p{L}\\p{M}*)+([\u0020\u00A0](?:\\p{L}\\p{M}*)+){1,3})");

if (regex.IsMatch(text)) {
	Console.WriteLine(text + " is the format of a three word address");
} else {
	Console.WriteLine(text + " is NOT the format of a three word address");
}
Copied
text = "index.home.raft"
regex = /\/*(?:[^0-9`~!@#$%^&*()+\-_=\[\{\]}\\|'<>.,?\/\";:£§º©®\s]{1,}[.。。・・︒។։။۔።।][^0-9`~!@#$%^&*()+\-_=\[\{\]}\\|'<>.,?\/\";:£§º©®\s]{1,}[.。。・・︒។։။۔።।][^0-9`~!@#$%^&*()+\-_=\[\{\]}\\|'<>.,?\/\";:£§º©®\s]{1,}|[<.,>?\/\";:£§º©®\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})/u

if text.match(regex)
  puts "#{text} is the format of a three word address"
else
  puts "#{text} is NOT the format of a three word address"
end
Copied
var text = 'index.home.raft';
var regex = RegExp(r"\/{0,}(?:[^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?\/"'"'r";:£§º©®\s]{1,}[.。。・・︒។։။۔።।][^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?\/"'"'r";:£§º©®\s]{1,}[.。。・・︒។։။۔።।][^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?\/"'"'r";:£§º©®\s]{1,}|[^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?\/"'"'r";:£§º©®\s]{1,}([\u0020\u00A0][^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?\/"'"'r";:£§º©®\s]+){1,3}[.。。・・︒។։။۔።।][^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?\/"'"'r";:£§º©®\s]{1,}([\u0020\u00A0][^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?\/"'"'r";:£§º©®\s]+){1,3}[.。。・・︒។։။۔።।][^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?\/"'"'r";:£§º©®\s]{1,}([\u0020\u00A0][^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?\/"'"'r";:£§º©®\s]+){1,3})");
  
if (regex.hasMatch(text)) {
    print('$text is the format of a three word address');
} else {
    print('$text is NOT the format of a three word address');
}
Copied
use regex::Regex;

fn main() {
    let text = "index.home.raft";
    let regex_match = r#"/*(?:[^0-9`~!@#$%^&*()+\-_=\[\{\]}\\|'<>.,?/;:£§º©®\s]{1,}[.。。・・︒។։။۔።।][^0-9`~!@#$%^&*()+\-_=\[\{\]}\\|'<>.,?/;:£§º©®\s]{1,}[.。。・・︒។։။۔።।][^0-9`~!@#$%^&*()+\-_=\[\{\]}\\|'<>.,?/;:£§º©®\s]{1,}|[^0-9`~!@#$%^&*()+\-_=\[\{\]}\\|'<>.,?/;:£§º©®\s]{1,}([\u0020\u00A0][^0-9`~!@#$%^&*()+\-_=\[\{\]}\\|'<>.,?/;:£§º©®\s]+){1,3}[.。。・・︒។։။۔።।][^0-9`~!@#$%^&*()+\-_=\[\{\]}\\|'<>.,?/;:£§º©®\s]{1,}([\u0020\u00A0][^0-9`~!@#$%^&*()+\-_=\[\{\]}\\|'<>.,?/;:£§º©®\s]+){1,3}[.。。・・︒។։။۔።।][^0-9`~!@#$%^&*()+\-_=\[\{\]}\\|'<>.,?/;:£§º©®\s]{1,}([\u0020\u00A0][^0-9`~!@#$%^&*()+\-_=\[\{\]}\\|'<>.,?/;:£§º©®\s]+){1,3})"#;
    let regex = Regex::new(regex_match).unwrap();

    if regex.is_match(text) {
        println!("{} is the format of a three-word address", text);
    } else {
        println!("{} is NOT the format of a three-word address", text);
    }
}
Copied
package main

import (
	"fmt"
	"regexp"
)

func main() {
	text := "index.home.raft"

	// Define the regular expression pattern
    pattern := `/*(?:[^0-9\x60~!@#$%^&*()+\-_=\[\{\]}\\|'<>.,?/;:£§º©®\s]{1,}[.。。・・︒។։။۔።।][^0-9\x60~!@#$%^&*()+\-_=\[\{\]}\\|'<>.,?/;:£§º©®\s]{1,}[.。。・・︒។։။۔።।][^0-9\x60~!@#$%^&*()+\-_=\[\{\]}\\|'<>.,?/;:£§º©®\s]{1,}|[^0-9\x60~!@#$%^&*()+\-_=\[\{\]}\\|'<>.,?/;:£§º©®\s]{1,}([\x{0020}\x{00A0}][^0-9\x60~!@#$%^&*()+\-_=\[\{\]}\\|'<>.,?/;:£§º©®\s]+){1,3}[.。。・・︒។։။۔።।][^0-9\x60~!@#$%^&*()+\-_=\[\{\]}\\|'<>.,?/;:£§º©®\s]{1,}([\x{0020}\x{00A0}][^0-9\x60~!@#$%^&*()+\-_=\[\{\]}\\|'<>.,?/;:£§º©®\s]+){1,3}[.。。・・︒។։။۔።।][^0-9\x60~!@#$%^&*()+\-_=\[\{\]}\\|'<>.,?/;:£§º©®\s]{1,}([\x{0020}\x{00A0}][^0-9\x60~!@#$%^&*()+\-_=\[\{\]}\\|'<>.,?/;:£§º©®\s]+){1,3})`
	// Compile the regular expression
	re, err := regexp.Compile(pattern)
	if err != nil {
		fmt.Println("Error compiling regex:", err)
		return
	}

	// Check if the text matches the pattern
	if re.MatchString(text) {
		fmt.Printf("%s is the format of a three-word address\n", text)
	} else {
		fmt.Printf("%s is NOT the format of a three-word address\n", text)
	}
}
Copied

🇻🇳 Special Considerations for Vietnamese Free Text

Vietnamese what3words addresses can be written with or without spaces.

When written with spaces:

  • The boundary for the end of the third word may be unclear, but an LLM would be almost certain to detect the boundary within free text
  • If the Vietnamese what3words address does not contain the /// prefix, the first word start boundary may be unclear, but an LLM would be almost certain to detect the boundary within free text.
  • If the Vietnamese what3words address contains the /// prefix, the first word start boundary is clear.

When written without spaces:

  • Treated the same as all other languages, no special case.

⚠️ If you’re not using an LLM and need to reliably detect Vietnamese addresses in free text, use our Delivery Notes Component for a more robust, logic-based solution.

🎯 Matching a Single, Exact Address (No Extra Text)

If you’re running the regex on a field that should only contain a single what3words address (and nothing else), add start (^) and end ($) anchors to your regex: ^word1.word2.word3$.

This ensures the match is strict, with no additional characters before or after.

🐍 Python-Specific Tips

  • For PythonYou might need to set the UTF-8 encoding explicitly reload(sys).setdefaultencoding("utf8") and # -*- coding: utf-8 -*- at the top of your script. Also, import re

🔧 Use Our API Wrappers

If you would like to employ this regex or check if the string is in the format of a 3 word address or if the string is a valid 3 word address, we have convenience functions in these API wrappers:

2Matching 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
public class What3WordsExample {
    public static void main(String[] args) {
        // Example what3words addresses
        String[] addresses = {"filled.count.soap", "not a 3wa", "not.3wa address"};

        // Check if the addresses are possible what3words addresses
        for (String address : addresses) {
            boolean isPossible = What3WordsV3.isPossible3wa(address);
            System.out.println("Is '" + address + "' a possible what3words address? " + isPossible);
        }
    }
}
Copied
import what3words

def main():
    # Initialize the What3Words API with your API key
    api_key = 'YOUR_API_KEY'
    w3w = what3words.Geocoder(api_key)

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

    # Check if the addresses are possible what3words addresses
    for address in addresses:
        is_possible = w3w.is_possible_3wa(address)
        print(f"Is '{address}' a possible what3words address? {is_possible}")

if __name__ == "__main__":
    main()
Copied
import W3WSwiftApi
import CoreLocation

func main() {
    // Initialize the What3Words API with your API key
    let api = What3WordsV4(apiKey: "YOUR_API_KEY")

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

    // Check if the addresses are possible what3words addresses
    for address in addresses {
        let isPossible = api.isPossible3wa(text: address)
        print("Is '\(address)' a possible what3words address? \(isPossible)")
    }
}

main()
Copied
<?php

require_once("vendor/autoload.php");

use What3words\Geocoder\Geocoder;

function main() {
    // Initialize the What3Words API with your API key
    $api = 'YOUR_API_KEY';
    $w3w = new Geocoder($api);

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

    // Check if the addresses are possible what3words addresses
    foreach ($addresses as $address) {
        $isPossible = $w3w->isPossible3wa($address);
        echo "Is '{$address}' a possible what3words address? " . ($isPossible ? "true" : "false") . "\n";
    }
}

main();

?>
Copied
using System;
using what3words.dotnet.wrapper;

namespace What3WordsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize the What3Words API with your API key
            var api = new What3WordsV3("YOUR_API_KEY");

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

            // Check if the addresses are possible what3words addresses
            foreach (var address in addresses)
            {
                bool isPossible = api.IsPossible3wa(address);
                Console.WriteLine($"Is '{address}' a possible what3words address? {isPossible}");
            }
        }
    }
}
Copied
require 'what3words'

def main
  # Initialize the What3Words API with your API key
  api_key = 'YOUR_API_KEY'
  w3w = What3Words::API.new(:key => api_key)

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

  # Check if the addresses are possible what3words addresses
  addresses.each do |address|
    is_possible = w3w.isPossible3wa(address)
    puts "Is '#{address}' a possible what3words address? #{is_possible}"
  end
end

if __FILE__ == $0
  main
end
Copied
void main() async {
  // Replace 'what3words-api-key' with your actual API key
  var api = What3WordsV3('what3words-api-key');

  List<String> addresses = ["filled.count.soap", "not a 3wa", "not.3wa address"];

  // Check if the addresses are possible what3words addresses
  for (String address in addresses) {
    bool isPossible = api.isPossible3wa(address);
    print("Is '$address' a possible what3words address? $isPossible");
  }
}
Copied
use what3words_api::What3words;

fn main() {
    // Initialize the what3words API with your API key
    let api_key = "YOUR_API_KEY_HERE";
    let w3w = What3words::new(api_key);

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

    // Check if the addresses are possible what3words addresses
    for address in &addresses {
        let is_possible = w3w.is_possible_3wa(address);
        println!("Is '{}' a possible what3words address? {}", address, is_possible);
    }
}
Copied
package main

import (
	"fmt"

	w3w "github.com/what3words/w3w-go-wrapper"
)

func main() {
	// Initialize the What3Words API with your API key
	apiKey := "<YOUR_API_KEY>"
	service := w3w.NewService(apiKey)

	// Example what3words addresses
	addresses := []string{"filled.count.soap", "not a 3wa", "not.3wa address"}

	// Check if the addresses are possible what3words addresses
	for _, address := range addresses {
		isPossible := service.IsPossible3wa(address)
		fmt.Printf("Is '%s' a possible what3words address? %t\n", address, isPossible)
	}
}
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.

3Verifying 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
public class What3WordsExample {
    public static void main(String[] args) {
		
		// Initialize the What3Words API with your API key
        What3WordsV3 api = new What3WordsV3("YOUR_API_KEY");
        
		// Example addresses
        String[] addresses = {
            "filled.count.soap",
    		"filled.count.",
    		"coding.is.cool"
        };

        // Check if the addresses are valid what3words addresses
        for (String address : addresses) {
            boolean response = api.isValid3wa(address);
            if (response.isSuccessful() && response.getIsValid()) {
                System.out.println(address + " is a valid what3words address");
            } else if (response.isSuccessful() && !response.getIsValid()) {
                System.out.println(address + " is an invalid what3words address");
            } else {
                System.out.println("isValid3wa error: " + response.getError().getKey() + " " +  response.getError().getMessage());
            }
        }
    }
}
Copied
import what3words

def main():
    # Initialize the what3words API with your API key
    api_key = 'YOUR_API_KEY'
    w3w = what3words.Geocoder(api_key)

    # Example addresses
    addresses = [
        "filled.count.soap",
        "filled.count.",
        "coding.is.cool"
    ]

    # Check if the addresses are valid what3words addresses
    for address in addresses:
        is_valid = w3w.is_valid_3wa(address)
        print(f"Is '{address}' a valid what3words address? {is_valid}")

if __name__ == "__main__":
    main()
Copied
import W3WSwiftApi
import CoreLocation

func main() {
    // Initialize the what3words API with your API key
    let apiKey = "YOUR_API_KEY"
    let api = What3WordsV4(apiKey: apiKey)

    // Example addresses
    let addresses = [
        "filled.count.soap",
        "filled.count.",
        "coding.is.cool"
    ]
	
	// Check if the addresses are valid what3words addresses
    for address in addresses {
      api.isValid3wa(words: address) { result in
        if result == true {
          print("'\(address)' is a valid what3words address")
        } else {
          print("'\(address)' is NOT a valid what3words address")
        }
      }
    }
}
main()
Copied
<?php

require_once("vendor/autoload.php");

use What3words\Geocoder\Geocoder;

function main() {
    // Initialize the what3words API with your API key
    $apiKey = 'YOUR_API_KEY';
    $w3w = new Geocoder($apiKey);

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

    // Check if the addresses are valid what3words addresses
    foreach ($addresses as $address) {
        $isValid = $w3w->isValid3wa($address);
        echo "Is '$address' a valid what3words address? " . ($isValid ? 'True' : 'False') . "\n";
    }
}
main();
Copied
using System;
using what3words.dotnet.wrapper;

namespace What3WordsExample

class Program
{
    static void Main(string[] args)
    {
        // Initialize the what3words API with your API key
        var api = new What3WordsV3("YOUR_API_KEY");

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

        // Check if the addresses are valid what3words addresses
        foreach (var address in addresses)
        {
            bool isValid = api.IsValid3wa(address);
            Console.WriteLine($"Is '{address}' a valid what3words address? {isValid}");
        }
    }
}
Copied
require 'what3words'

def main
  # Initialize the what3words API with your API key
  api_key = 'YOUR_API_KEY'
  w3w = What3Words::API.new(:key => api_key)

  # Example addresses
  addresses = [
    "filled.count.soap",
    "filled.count.",
    "coding.is.cool"
  ]

  # Check if the addresses are valid what3words addresses
  addresses.each do |address|
    is_valid = w3w.isValid3wa(address)
    puts "Is '#{address}' a valid what3words address? #{is_valid}"
  end
end

if __FILE__ == $0
  main
end
Copied
void main() async {
  // Replace 'what3words-api-key' with your actual API key
  var api = What3WordsV3('what3words-api-key');

  List<String> w3w_addresses = [
    "filled.count.soap",
    "filled.count.",
    "coding.is.cool"
  ];

  // Iterate over the list and check if each address is valid
  for (String w3w_address in w3w_addresses) {
    var response = await api.isValid3wa(w3w_address);

    if (response.isSuccessful() && response.isValid == true) {
      print("$w3w_address is a valid what3words address");
    } else if (response.isSuccessful() && response.isValid == false) {
      print("$w3w_address is an invalid what3words address");
    } else {
      print("isValid3wa error: ${response.error?.code} - ${response.error?.message}");
    }
  }
}
Copied
use what3words_api::What3words;

fn main() {
    // Initialize the what3words API with your API key
    let api_key = "YOUR_API_KEY_HERE";
    let w3w = What3words::new(api_key);

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

    // Check if each address is a valid what3words address
    for address in addresses {
        let is_valid = w3w.is_valid_3wa(address);
        println!("Is '{}' a valid what3words address? {}", address, is_valid);
    }
}
Copied
package main

import (
	"context"
	"fmt"

	w3w "github.com/what3words/w3w-go-wrapper"
)

func main() {
	// Initialize the What3Words API with your API key
	apiKey := "<YOUR_API_KEY>"
	service := w3w.NewService(apiKey)

	// Example addresses
	addresses := []string{
		"filled.count.soap",
		"filled.count.",
		"coding.is.cool",
	}

	// Check if the addresses are valid what3words addresses
	for _, address := range addresses {
		isValid, err := service.IsValid3wa(context.Background(), address)
		if err != nil {
			fmt.Printf("Error validating address '%s': %v\n", address, err)
		} else {
			fmt.Printf("Is '%s' a valid what3words address? %t\n", address, isValid)
		}
	}
}
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.

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