Field calculator - regular expressions

09-25-2017 03:31 AM
New Contributor II

I'm beginner in ArcGIS Pro (I just switched from QGIS) and have some problem with some a little more advanced operation in field calculator. I would like to know how to use REGEX in field calculator to extract particular part of the string.

The string if in "Adr_for" field and in field "Number" I want to have extracted part.

Examples of the strings in "Adr_for":
13-0222222-1-02226-249A - -
13-022222-1-02225-33 - -

Expected result in "Number":

Regex expression (which worked in QGIS):

I tried to use something like this in ArcGIS PRO field calculator but it didn't work.


Code Block:
import re
def regex(Adr_for):
    return"""((\d+(?!(\d|-))\w)|(\d+(?!(\d|-))))""", Adr_for)

How should I do this to achieve expected result?

0 Kudos
5 Replies
MVP Legendary Contributor

I presume your example is conincidently too simple to expect


to work then?

New Contributor II

It's very nice solution but not perfect. It leaves empty spaces in field after values '33 ' and '249A ' (because between value and '-' there is space). I know I can fix it with some other basic function by second calculation pass but I would like to achieve that with one pass using regex, because it's something I used very often in QGIS.

BTW Is there any documents with description of all basic functions (like .split) of Field Calculator?

MVP Esteemed Contributor

For the doc. on a few examples of Calculate Field, you could check Calculate Field examples—Data Management toolbox | ArcGIS Desktop 

Think Location
MVP Legendary Contributor

enclose within strip


Esri Regular Contributor

It will work, I suspect all the raw string triple quoting is doing something, just in the Python window all is good:

import re
p = re.compile('((\d+(?!(\d|-))\w)|(\d+(?!(\d|-))))')
Adr_for = '13-0222222-1-02226-249A - -',Adr_for)
<_sre.SRE_Match object; span=(19, 23), match='249A'>

0 Kudos