Question

How to use Regex to split postcode if no space is provided?

  • 6 December 2021
  • 3 replies
  • 486 views

Hi folks, I’ve got a regex expression splitting UK postcodes so I can use just the first half of it in a zap and it works perfectly until someone enters a postcode without a space. The working regex is:

(?P<Incode>\w+)(?P<outcode>\s\w{1}\w{2})

So OL10 4TQ comes back with OL10 in the incode fine… but OL104TQ doesn’t match.

Can anyone help me work out a fix for this or propose a different solution? Thanks so much


This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

3 replies

Userlevel 7
Badge +14

Hi @PeterCera 

Can you specify the logic you are trying to apply?

Are you just trying to get the first 4 digits of an entered value?

Hi @Troy Tessalone I am trying to get the first half, which could be 2-4 alphanumeric characters:

E1 1AA / E11AA

EC1A 1AA / EC1A1AA

E11 1AA / E111AA

All above could be a properly formatted input, and I need to get E1, EC1A or E11.  The most accurate way could be to count 3 characters back, since all postcodes are always ending  0AA / digit-letter-letter

Hope that helps!
 

Userlevel 7
Badge +14

@PeterCera 

Why not try to truncate the last 3 digits then?

 

Try using a Code step: https://zapier.com/apps/code/help

 

JavaScript Code

 

RESULTS