4

Can I create a field calc expression in ArcGIS 10.3 to print a selection between special characters?

I would like to extract a selection of string from field [FolderPath] to a new field [Name]. The string is between a hyphen and a forward slash.

What I have in field [FolderPath] example string: 264K - Name of Place/FeatureType

What I want in field [Name] target string: Name of Place

10

Solution with re module

You can use the built-in re module in Python for that.

import re
sr = re.split(' - |/',s)[1]

Field calculator view (where FeatureStr is the source field with 264K - Name of Place/FeatureType and Split is the output field with : Name of Place.

enter image description here

Pre-Logic Script Code:

def splitText(field):
  import re
  return re.split(' - |/', field)[1]

Field to be equal to:

splitText(!FeatureStr!)

Alternative solution with no re module:

!FeatureStr!.split(' - ')[1].split('/')[0]
  • Glad it works for you. Regex is a powerful tool which can be helpful in many cases when you need to split various strings. It might be a bit hard to master it, so when need just a quick fix - you can use the str.split() method multiple times iterating results - !FeatureStr!.split(' - ')[1].split('/')[0] would also work. And you wouldn't need a definition block. I've updated my answer. – Alex Tereshenkov Nov 11 '15 at 16:48
  • Is importing inside the function necessary, or can you put it outside? – jpmc26 Nov 12 '15 at 1:24
1

You can go to 'field calculator > String functions'. Select the Left() function. Inside bracket of left:

Left(Column_name, No. of characters to extract)

Hit Apply.

0

I had a similar need to extract a variable length string from within a longer string, but where the left and right lengths of characters to be removed from the source string were fixed (but not necessarily the same).

I used this expression in the field calculator for the new string:

Left(RIGHT([field],Len([field] )-N1),Len(RIGHT([field],Len([field] )-N1))-N2)

Where:

  • [field] = source string
  • N1 = number of characters to remove from the left of the string
  • N2 = number of characters to remove from the right of the string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.