ArcGIS Desktop – Removing Numeric Characters from Alphanumeric Field

arcgis-10.0arcgis-desktopfield-calculatorpython-parser

I have a field containing alphanumeric values in the following format:

A-AA00

Sometimes they are also

A-AAA0

I need a Python script I can put in the field calculator in ArcMap 10 that will at least remove the numeric characters, and retrain the alphabetical ones and the hyphen. So that, for example, this:

A-AA0

becomes this:

A-AA

I also want to replace the numeric chars with numeric chars from another field. This Stack Exchange entry addresses this issue, but the string has a space before the number and this can be used for splitting and indexing… my strings are not so handily constructed.

Best Answer

I would use regular expressions. If you put the following in the code block section of the calculator it will return a string that has all digits stripped, no matter were they are:

import re
def strip_digits(s):
    return re.sub("\d+", "", s)

This can then be called from the calculate box as:

strip_digits(!column_name!)

If you want to replace characters, add another argument to the function for the replacement characters, and put it in place of the empty string.