# Using Python Function in Field Calculator to find numbers

Disclaimer, I am terrible at writing my own functions especially in the field calculator. I have an address field (Address) with records that are missing street numbers. I would like to find these records, skip over them, and populate a field (Number) with the first element of the records that do have a street number. I would like to do this in the field calculator if possible (ArcMap 10.2.2, Basic).

Here's what I got so far:

``````def findNumbers(Address):
pass

Number =
def findNumbers(!Number!)
``````
• I see, but It's in the field calculator which requires a python syntax that is specific to calculations done in ArcMap. How would a non ArcMap python programmer be able to answer this? Feb 13, 2017 at 19:59

Regular expressions are nice for extraction.

``````import re
def extract(s):
num = re.search('^\d+', s)  # find all leading digits
if num is None:
return None
else:
return int(num.group())
``````
• import before def ? Feb 13, 2017 at 20:54
• @FelixIP, ideally yeah you would import in the python window.
– Paul
Feb 13, 2017 at 20:57
• @FelixIP, never realized the code block could have more than a def statement. It only runs that initially (e.g `import time;time.sleep(5)` adds only 5s to runtime not 5x#rows). Good to know!
– Paul
Feb 14, 2017 at 22:25
• @Paul I take it to extremes sometimes, gis.stackexchange.com/questions/193681/… Feb 14, 2017 at 23:08
• @FelixIP, that's awful, haha. +1 though for abusing field calculator.
– Paul
Feb 14, 2017 at 23:28

This question is most certainly a duplicate, but anyway I'd use:

``````def getNumber(aString):
aList=aString.split();n=len(aList)
if n>1:
try: aNumber= int(aList[0]);return aNumber
except: return -1
else: return -1
#-----------------------