5

I want to extract the area code from a UK postcode using a regular expression. For example this would be removing "SW" from "SW11 1AW". The area is always the first characters of the string and is always followed by a number. I can't just extract the first two characters as sometimes there is only one letter, eg "E1 4PN". So it needs to match A-Z only from the start of the string until it hits a number and return just the letters. For the sake of argument the string will always be upper case.

Thanks.

Spudley
  • 166,037
  • 39
  • 233
  • 307
artparks
  • 761
  • 1
  • 10
  • 24
  • PHP - apologies, assumed this would be fairly agnostic. – artparks Jul 25 '13 at 09:19
  • be aware that there are valid postcodes that don't stick to the standard layout. Granted they're generally fairly obscure, but it does include BFPO (armed forces mail) as well as various UK territories (Gibraltar, Falklands, anyone?) – Spudley Jul 25 '13 at 09:19
  • 1
    You might want to read some of the answers here: http://stackoverflow.com/questions/164979/uk-postcode-regex-comprehensive – Spudley Jul 25 '13 at 09:20
  • 1
    @artparks - re language: Regex syntax is generally similar between languages but it is not standardised, and does differs sufficiently from one language to the next that it's always a good idea to be specific. An answer for one language might be valid for another, or might need adapting slightly. Also when people ask for regex help, there are often non-regex alternatives, and knowing the language involved will help with giving answers for them too. – Spudley Jul 25 '13 at 09:22
  • @Spudley, worth knowing, thanks. I'm not going to concern myself with obscure postcodes, the usage here need only be fairly simple. – artparks Jul 25 '13 at 09:39

3 Answers3

2

(assuming PHP)

$letters = preg_replace('#^([a-z]+).*#i','$1',$postcode);
MDEV
  • 10,730
  • 2
  • 33
  • 49
  • If I do this preg_replace('#^([a-z]+).+#i', '$1', 'SA') it returns S – darrenwh Jan 19 '18 at 11:15
  • Of course, this is currently assuming a full postcode is being passed, thought it could be made more defensive by changing the last `+` to a `*`. Will update my answer accordingly – MDEV Jan 22 '18 at 11:04
1

In ruby this would look like:

postcode = 'SW11 1AW'
postcode[/^[a-z]+/i] # get the area code
#=> "SW"
postcode[/^[a-z]+(.*)/i,1] # get the rest
#=> "11 1AW"

Note: The i flag (ignore case) is set. So both, uppercase and downcase letters work.

tessi
  • 13,313
  • 3
  • 38
  • 50
1
^(?i)[a-z]+(?=\d)

Will find first two letters if there is two or first one if only one letter that is before a number, regardless of case.

Srb1313711
  • 2,017
  • 5
  • 24
  • 35