Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 736797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:38:48+00:00 2026-05-14T07:38:48+00:00

I need to re-format a list of UK postcodes and have started with the

  • 0

I need to re-format a list of UK postcodes and have started with the following to strip whitespace and capitalize:

postcode.upcase.gsub(/\s/,'')

I now need to change the postcode so the new postcode will be in a format that will match the following regexp:

^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$

I would be grateful of any assistance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:38:48+00:00Added an answer on May 14, 2026 at 7:38 am

    If this standards doc is to be believed (and Wikipedia concurs), formatting a valid post code for output is straightforward: the last three characters are the second part, everything before is the first part!

    So assuming you have a valid postcode, without any pre-embedded space, you just need

    def format_post_code(pc)
      pc.strip.sub(/([A-Z0-9]+)([A-Z0-9]{3})/, '\1 \2')
    end
    

    If you want to validate an input post code first, then the regex you gave looks like a good starting point. Perhaps something like this?

    NORMAL_POSTCODE_RE = /^([A-PR-UWYZ][A-HK-Y0-9][A-HJKS-UW0-9]?[A-HJKS-UW0-9]?)\s*([0-9][ABD-HJLN-UW-Z]{2})$/i
    GIROBANK_POSTCODE_RE = /^GIR\s*0AA$/i
    def format_post_code(pc)
      return pc.strip.upcase.sub(NORMAL_POSTCODE_RE, '\1 \2') if pc =~ NORMAL_POSTCODE_RE
      return 'GIR 0AA' if pc =~ GIROBANK_POSTCODE_RE
    end
    

    Note that I removed the ‘0-9’ part of the first character, which appears unnecessary according to the sources I quoted. I also changed the alpha sets to match the first-cited document. It’s still not perfect: a code of the format ‘AAA ANN’ validates, for example, and I think a more complex RE is probably required.

    I think this might cover it (constructed in stages for easier fixing!)

    A1  = "[A-PR-UWYZ]"
    A2  = "[A-HK-Y]"
    A34 = "[A-HJKS-UW]"        # assume rule for alpha in fourth char is same as for third
    A5  = "[ABD-HJLN-UW-Z]"
    N   = "[0-9]"
    AANN = A1 + A2 + N + N     # the six possible first-part combos
    AANA = A1 + A2 + N + A34
    ANA  = A1 + N + A34
    ANN  = A1 + N + N
    AAN  = A1 + A2 + N
    AN   = A1 + N
    PART_ONE = [AANN, AANA, ANA, ANN, AAN, AN].join('|') 
    PART_TWO = N + A5 + A5
    
    NORMAL_POSTCODE_RE = Regexp.new("^(#{PART_ONE})[ ]*(#{PART_TWO})$", Regexp::IGNORECASE)  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a list in the following format: List<Amortizacion> lista = new List<Amortizacion> {
I have a list need to be format it to SQL scripte list =
I have a list of tuples of format [(String,String)] and I need a function
I need to parse a the following list format into a dict. This represents
I have a list in the following format 77 Infinite Dust 4 Illusion Dust
I need to format an array from a list of values. Currently I have
I have a list in Excel and I need to format rows based on
Have a LINQ result, need to format it in plain text nicely to send
i have List of int which consists of value 0,0,0,1,2,3,4,0,0 now i like to
I need to generate a contacts list in CSV format, as if exported by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.