You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Inserting a “–” or a “0” using REGEX?

The format of the British outward postcode is AA for the Area and 99 for the District.

Most outward postcodes have the format AA99.

However some postcodes have either the format A99 or the format AA9.


For sorting and searching purposes the formats A99 and AA9 have to be “adjusted” (extended) to A99 and AA09.

When the postcode format is entered as AA99, then get the postcode as entered: AA99

When the postcode format is entered as A99, then insert a “–“ after the first letter: A–99

When the postcode format is entered as AA9, then insert a “o“ after the second letter: AA09


The first table contains the ranges of British postcodes, 66 rows in total (only a selection is shown).


The second table looks up the first table for either GB✷1 or GB✷2 based on the entered postcode.


As an example, entering the postcode HP7 in the second table results in GB✷1 after searching the first table. 


My question: is it possible to replace the 4 formulas in the cells D3, E3, F3 and G3 of the second table with a simple formula in 1 cell based on REGEX?


Paul.

iMac 27″, macOS 10.14

Posted on Aug 25, 2020 11:56 AM

Reply
Question marked as Top-ranking reply

Posted on Aug 25, 2020 6:55 PM

You'll have to double check that the formula below meets all your criteria and actually works. There are multiple ways to do this. This one is the simplest of the ones I came up with.


cell H2 =LEFT(REGEX.EXTRACT(D2,"[a-zA-Z]+")&"-",2)&RIGHT("0"&REGEX.EXTRACT(D2,"[0-9]+"),2)


It does the following:

  1. Extracts the letter(s)
  2. Appends a "-" onto them/it
  3. Truncates the resulting string to the leftmost two characters. If there had been only one letter it is now a letter and a "-". If it was two letters, the "-" gets removed, leaving the two characters.
  4. Extracts the number(s)
  5. Prepends a "0" onto them/it
  6. Truncates the resulting string to the rightmost two characters. If there had been only one number it is now a "0" and a number. If it was two numbers, the leading "0" gets removed, leaving the two numbers.
  7. Sticks those two strings together.
13 replies
Question marked as Top-ranking reply

Aug 25, 2020 6:55 PM in response to stfflspl

You'll have to double check that the formula below meets all your criteria and actually works. There are multiple ways to do this. This one is the simplest of the ones I came up with.


cell H2 =LEFT(REGEX.EXTRACT(D2,"[a-zA-Z]+")&"-",2)&RIGHT("0"&REGEX.EXTRACT(D2,"[0-9]+"),2)


It does the following:

  1. Extracts the letter(s)
  2. Appends a "-" onto them/it
  3. Truncates the resulting string to the leftmost two characters. If there had been only one letter it is now a letter and a "-". If it was two letters, the "-" gets removed, leaving the two characters.
  4. Extracts the number(s)
  5. Prepends a "0" onto them/it
  6. Truncates the resulting string to the rightmost two characters. If there had been only one number it is now a "0" and a number. If it was two numbers, the leading "0" gets removed, leaving the two numbers.
  7. Sticks those two strings together.

Aug 27, 2020 9:08 AM in response to stfflspl

stfflspl wrote:
The following regular expression can be used for the purpose of validation:
^[A-Z]{1,2}[0-9][A-Z0-9]? ?[0-9][A-Z]{2}$


If I read that right, the leading ^ and trailing $ are the same as where we use quotes. The first part of the code is, in order

  1. One or two characters from A-Z
  2. One number 0-9
  3. Zero or one character A-Z or 0-9


That corresponds to the 6 possibilities you listed. I think the more recent formula covers all those bases.

Aug 27, 2020 7:31 AM in response to Badunit

The format of (my actual) interest is the first part of the format (“Area”&”District") for getting an internal destination code: either GB*1 or GB*2.


This format might be:

A9

A99

A9A

AA9

AA99

AA9A


From Wikipedia:

Postcodes in the United Kingdom

A postcode can be validated against a table of all 1.7 million postcodes in Code-Point Open. The full delivery address including postcode can be validated against the Royal Mail Postcode Address File (PAF), which lists 29 million valid delivery addresses,[25] constituting most (but not all) addresses in the UK.[26]


The following regular expression can be used for the purpose of validation:

^[A-Z]{1,2}[0-9][A-Z0-9]? ?[0-9][A-Z]{2}$


The following regular expression can be used for the purpose of validation and includes postcode formats from Special Cases:

^(([A-Z]{1,2}[0-9][A-Z0-9]?|ASCN|STHL|TDCU|BBND|[BFS]IQQ|PCRN|TKCA) ?[0-9][A-Z]{2}|BFPO ?[0-9]{1,4}|(KY[0-9]|MSR|VG|AI)[ -]?[0-9]{4}|[A-Z]{2} ?[0-9]{2}|GE ?CX|GIR ?0A{2}|SAN ?TA1)$


Paul.




Aug 27, 2020 2:51 AM in response to stfflspl

Ahh, the problem with numbering systems: exceptions to the rules. That one doesn't fit the specified pattern. But no worries. Remember I said I had come up with several ways to do the same thing? Luckily, one of them handles this situation.


=LEFT(TEXTBEFORE(C3,REGEX("[0-9]"))&"-",2)&RIGHT("0"&TEXTAFTER(C3,REGEX("[a-zA-Z]+")),2)


The left part of the string is determined by location of the first number. Everything to the left of the first number is part of the left string.


The right part of the string is everything to the right of the first set of alpha characters. It is the first non-alpha character plus everything to the right of it.


Aug 27, 2020 11:12 AM in response to Badunit

For sorting/searching purpose the format of a manually entered postcode (first two-to-four characters) has to be “updated” to:


A9 –> A–09

A99 –> A99

A9A –> A90A

AA9 –> AA09

AA99 –> AA99

AA9A –> AA9A


I checked all formats. The tested postcodes are “adjusted” as expected.

Again, many thanks for the formula.


Paul.

Inserting a “–” or a “0” using REGEX?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.