-4

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());
        }
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Alex
  • 71
  • 10
  • possible duplicate of [Reguar Expression, UK Post Code, Slight Amendment?](http://stackoverflow.com/questions/4195800/reguar-expression-uk-post-code-slight-amendment) – Ron Rosenfeld Nov 30 '14 at 17:47
  • Why did none of the questions that were returned when you searched StackOverflow for **UK Post Codes** satisfy your requirements? What have you tried and what was the problem with your attempted solutions? – Ron Rosenfeld Nov 30 '14 at 17:48
  • Ron UK postcodes are easy. I need a regexp genius that can create me a LONDON postcode regex – Alex Nov 30 '14 at 17:51
  • Why can't you just search for anything that matches the particular prefixes that apply to London? Once you've identified a substring as being a post code, merely look at the first 2-4 characters and see if they match something from the list. – Ron Rosenfeld Nov 30 '14 at 17:53
  • Tell me Ron how would I do that for London only – Alex Nov 30 '14 at 17:54
  • What have you tried and what was the problem with your attempted solutions? I find it hard to believe that Java cannot check the first 2,3 or 4 characters of a substring, and compare them against a list. – Ron Rosenfeld Nov 30 '14 at 17:57
  • Yes ron but I want it all in a regexp. It's too costly in terms of performance to compare them to a list – Alex Nov 30 '14 at 18:00
  • 1
    By the way, the downvote was due to the complete absence in your post of an attempted solution, and statement as to the problems you had run into. This is not a code-writing service. – Ron Rosenfeld Nov 30 '14 at 18:00
  • @Alex: `if (londonCodesList.contains(postcode)) {` – maksimov Nov 30 '14 at 18:34

4 Answers4

1

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!");
    }
}
maksimov
  • 5,792
  • 1
  • 30
  • 38
1

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.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

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).

Doogal
  • 1,637
  • 2
  • 18
  • 22
0

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());
        }
    }
}
Alex
  • 71
  • 10