Regular expression for London only postcodes

815 views Asked by At

London is a good starting point but if you are aware of regular expressions or regex for UK counties such as Surrey it would be hugely appreciated. Basically I'm creating a web crawler but the information I'm retrieving only gives a London street name, borough and postcode. It does not say London.

Here is a site that gives the London postcodes @droogal

Is there a library of regular expressions that covers London postcodes. If you need to write it, the first part of the postcode gives away the location i.e. London.

There is:

WC, EC, E1-E20, N1-N22, NW1-NW11, SE1-SE28, SW1-SW20, W1-14 and Greater London ... BR, CR, DA,EN, HA, IG, KT, RM, SM, TW, UB, WD.

The language I'm using is Java but saying that regular expressions are found in most languages!

This is what I got so far. How do I do number ranges in regex?

public static void main(String[] args) throws IOException {

    String postcode = "WD";
     Pattern regex = Pattern.compile("^(WC|EC|BR|CR|DA|EN|HA|IG|KT|RM|SM|TW|UB|WD)");
    Matcher finder = regex.matcher(postcode);
    if (finder.find()) {
        try {
            String value = finder.group(0);
            System.out.println("This is London");
        } catch (NumberFormatException e) {
            System.out.println(e.getMessage());
        }
    }
}
4

There are 4 answers

0
Bohemian On

Assuming you have a post code, this matches a London one:

boolean isLondon = postcode.matches(".*(WC|EC|BR|CR|DA|EN|HA|IG|KT|RM|SM|TW|UB|WD).*");

The small thing to note is that the regex passed to String.matches() must match the whole string to return true.

0
Alex On

This is the answer I wanted.

The following Regular expression matches the first part of any UK postcode to determine whether it is a London one. Obviously this is one of my first attempts at regular expressions but I've tested every borough

public static void main(String[] args) throws IOException {

    String postcode = "BR1 1AA";

 //Revised
     Pattern regex = Pattern.compile("(WC|EC|BR|CR|DA|EN|HA|IG|KT|RM|SM|TW|UB|WD|(E[1-9]|E1[1-9]|E20)"
                                    + "|(N[1-9]|N1[1-9]|N2[0-2])|(NW[1-9]|NW1[0-1])"
                                    + "|(SE[1-9]|SE1[1-9]|SE2[0-8])|(W[1-9]|W1[1-4])"
                                    + "|(SW[1-9]|SW1[1-9]|SW20))");
    Matcher finder = regex.matcher(postcode);
    if (finder.find()) {
        try {
            String value = finder.group(0);
            System.out.println("This is London");
        } catch (NumberFormatException e) {
            System.out.println(e.getMessage());
        }
    }
}
0
maksimov On

I struggle with regular expressions:

private static final List LONDON_CODES = Arrays.asList("WC","EC","BR","CR","DA","EN","HA","IG","KT","RM","SM","TW","UB","WD");

public static void main(String ... a) {
    if(LONDON_CODES.contains("WD")) {
        System.out.println("This is London!");
    } else {
        System.out.println("This is country!");
    }
}
0
Doogal On

I don't think your proposed solution will give you what you want (apologies that my site has led you astray!). Not all of the postcodes in those outer London boroughs are within the boundary of London. For example KT1 is Kingston, which is part of Greater London, KT24 is East Horsley in Surrey, which is definitely not part of London. Your best bet is to download the full postcode dataset from ONS/Ordnance Survey/my site and pull out the postcodes that are in a London Borough or have the Built Up Area set as Greater London (I'm not sure if these are equivalent but I'd guess they are).