HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Regular Expressions / Java Regex – UK Postcode Validation

Java Regex – UK Postcode Validation

In this java regex tutorial, we will Learn to use regular expressions to validate postal zip codes specific to UK. You can modify the regex to suit it for any other format as well.

1. What are valid UK postal codes

Postal codes in the U.K. (or postcodes) are composed of five to seven alphanumeric characters separated by a space. Those two parts are the outward code and the inward code.

The outward code includes the postcode area and the postcode district. The inward code includes the postcode sector and the postcode unit.

Examples of postcodes include “SW1W 0NY”, “PO16 7GZ”, “GU16 7HF”, or “L1 8JQ”.

The rules covering which characters can appear at particular positions are little complicated and filled with exception cases. The regular expression given here therefore sticks to the basic rules only.

Regex: ^[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{2}$

For checking the validation rules on UK postal codes, follow this wikipedia page.

2. UK Postcode Validation Example

List<String> zips = new ArrayList<String>();
      
//Valid ZIP codes
zips.add("SW1W 0NY");  
zips.add("PO16 7GZ");  
zips.add("GU16 7HF");  
zips.add("L1 8JQ");  

//Invalid ZIP codes
zips.add("Z1A 0B1");
zips.add("A1A 0B11");

String regex = "^[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{2}$";

Pattern pattern = Pattern.compile(regex);

for (String zip : zips)
{
	Matcher matcher = pattern.matcher(zip);
	System.out.println(matcher.matches());
}

Output:

true
true
true
true

false
false

Feel free to drop your questions related to above UK postcode validation example.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java Regex Tutorial

  • Java Regex – Introduction
  • Java Regex : Alphanumeric
  • Java Regex : Credit Card Numbers
  • Java Regex : Canadian Postcodes
  • Java Regex : Currency Symbol
  • Java Regex : Date Format
  • Java Regex : Email Address
  • Java Regex : Password
  • Java Regex : Greek Characters
  • Java Regex : ISBNs
  • Java Regex : Min/Max Length
  • Java Regex : No. of Lines
  • Java Regex : No. of Words
  • Java Regex : SSN
  • Java Regex : UK Postal Codes
  • Java Regex : US Postal Codes
  • Java Regex : Trademark Symbol
  • Java Regex : Intl Phone Numbers
  • North American Phone Numbers

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces