So I have addresses that have a zip in one field, and I want to extract the zip to a new field. Literally, the simplest regex that you can do.
This works fine in Python and returns 97321:
import re
address = '97321'
postal_code = re.search(r'.*(\d{5}(\-\d{4})?)$', address)
print(postal_code.group())
In Pro, in Calculate Field it acts like postal_code is None every single time, but I dont know because there is no way to properly debug this so I have no idea what is wrong. So please tell me what the secret trick is to get regex to work like it is supposed to in every other piece of software on earth.
Solved! Go to Solution.
It doesn't like it when you have text after the zip.
So, any spaces, any letters, etc.
I fixed it in my test data by getting rid of the $.
I also got rid of the .*, since it ends up grabbing any text in front.
def extract(address):
import re
postal_code = re.search(r'(\d{5}(\-\d{4})?)', address).group(0)
return postal_code
Dumb question, but are they both text fields?
It worked fine for me when both were text.
Yes, both are Text Fields that's helpful though. Thought I was going crazy.
It doesn't like it when you have text after the zip.
So, any spaces, any letters, etc.
I fixed it in my test data by getting rid of the $.
I also got rid of the .*, since it ends up grabbing any text in front.
def extract(address):
import re
postal_code = re.search(r'(\d{5}(\-\d{4})?)', address).group(0)
return postal_code