Well, I agree with one of the comments above about using Maplex if that will work for you. If it doesn't though, what you could do is use the field calculator as you suggest. For the all initials (J H S & L L S) You would want to do something like the following:
Code Block / Pre-Logic Script Code:
def nameCalc(inName):
outName = ""
namesList = inName.split("&")
for name in namesList:
outName = outName + " &"
nameL = name.split()
for word in nameL:
outName = outName + " " + word[0]
outName = outName.strip(" &")
return outName
and then in the expression you would put nameCalc(!Owner_Nm!)
You could select the parcels you want just initials for and then run that. However, if you want to do them all at once and you have an area field or such as that that you want to use to automate the process (ex: all parcels larger than or equal to 5 acres get full name, between 5 and 1 acres get abreviated, and smaller than 1 gets just initials), then you could do something like the following in the code block:
def nameCalc(inName,area):
outName = ""
if area < 1:
namesList = inName.split("&")
for name in namesList:
outName = outName + " &"
nameL = name.split()
for word in nameL:
outName = outName + " " + word[0]
outName = outName.strip(" &")
if area >= 1 and area < 5:
namesList = inName.split("&")
for name in namesList:
outName = outName + " &"
nameL = name.split()
for word in nameL:
if word != nameL[-1]:
outName = outName + " " + word[0]
else:
outName = outName + " " + word
if area >= 5:
outName = inName
outName = outName.strip(" &")
return outName
and then in the expression you would put nameCalc(!Owner_Nm!,!Acres!)
PLEASE NOTE THOUGH: if you do that, you would A) Have to replace the 1, 1, 5, & 5 with the appropriate values for your data and what you want and B) you would have to replace !Acres!
in the expression with whatever area/size field you wanted to use and that field would have to be a numeric field for the code above to run correctly.
I know that's a lot of code, and it may be a bit to work your way through, but it should return each of the 3 desired results and will correctly handle both single names and double names separated by &
I hope that helps, and if you have questions, feel free to comment and if I can help, I will try to.
Best Answer
FOR QGIS:
You can use below expressions:
N.B. I am surprised that qgis does not support negative look behind though python 2.7 fully supports it.
FOR ArcGIS:
Basically, this answer is equally applied for ArcGIS and QGIS. I will use regular expression to solve the problem
I would use below function in the arcmap field calculator:
or below if there is a single space already before any capital letter in the passed text.
Example:
Explanation:
I used
re.sub(r'([A-Z])',r" \1",text,re.MULTILINE).strip()
lets explain it.re.sub
just grabs all that matches([A-Z])
from the inputtext
and replaces it with the exactly that character but with leading space but it, thus, adds a leading space in the beginning of the line which is unwanted.So I usedstrip()
function to remove unwanted leading and trailing spaces.Explanation of the regular expression
([A-Z])
: It grabs all single character capital letters.Explanation of the regular expression
r \1
: Herer
is just a formatting flag and inspace+\1
,\1
is called backreference which returns the character grabbed by the previous regular expression.So replacing text is the exact Capital letters that grabbed by the previous regex plus the leading space.