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

  • Home
  • SEARCH
  • 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 92011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:07:11+00:00 2026-05-10T23:07:11+00:00

I am trying to use regular expressions to find a UK postcode within a

  • 0

I am trying to use regular expressions to find a UK postcode within a string.

I have got the regular expression working inside RegexBuddy, see below:

\b[A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2}\b 

I have a bunch of addresses and want to grab the postcode from them, example below:

123 Some Road Name
Town, City
County
PA23 6NH

How would I go about this in Python? I am aware of the re module for Python but I am struggling to get it working.

Cheers

Eef

  • 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. 2026-05-10T23:07:12+00:00Added an answer on May 10, 2026 at 11:07 pm

    repeating your address 3 times with postcode PA23 6NH, PA2 6NH and PA2Q 6NH as test for you pattern and using the regex from wikipedia against yours, the code is..

    import re  s='123 Some Road Name\nTown, City\nCounty\nPA23 6NH\n123 Some Road Name\nTown, City'\     'County\nPA2 6NH\n123 Some Road Name\nTown, City\nCounty\nPA2Q 6NH'  #custom                                                                                                                                                print re.findall(r'\b[A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2}\b', s)  #regex from #http://en.wikipedia.orgwikiUK_postcodes#Validation                                                                                             print re.findall(r'[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z]{2}', s) 

    the result is

    ['PA23 6NH', 'PA2 6NH', 'PA2Q 6NH'] ['PA23 6NH', 'PA2 6NH', 'PA2Q 6NH'] 

    both the regex’s give the same result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 68k
  • Answers 68k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I'm assuming that you are using the jQuery validation plugin.… May 11, 2026 at 12:11 pm
  • added an answer A search for Java and vcard yields quite a few… May 11, 2026 at 12:11 pm
  • added an answer You can pass the click handler a callback, which would… May 11, 2026 at 12:11 pm

Related Questions

I am trying to use regular expressions to find a UK postcode within a
I am trying to use MinGW to compile a C program under Windows XP.
I am trying to use a stringstream object in VC++ (VStudio 2003) butI am
I am trying to use the InternalsVisibleTo assembly attribute to make my internal classes
I am trying to use forms authentication with Active Directory but I need roles
I am trying to use Zend_Db_Select to write a select query that looks somewhat
I am trying to use TemplateToolkit instead of good ole' variable interpolation and my
I am trying to use Validation in WPF. I created a NotNullOrEmptyValidationRule as shown
I am trying to use Difflib.SequenceMatcher to compute the similarities between two files. These
I am trying to use the BGGA closures prototype with an existing JDK 6

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.