I have a field, and it has many words in it for every row and I want to insert a space before every capital letter except of the first one.

Example: I have something like this:



George smithJohnMike

And I want to modified it some thing like this:

George John Mike


George smith John Mike

How can I do it using QGIS or in ArcGis?

4 Answers 4



You can use below expressions:

replace((regexp_replace("YourField", '(?!^)([A-Z])', ' \\1')),'  ', ' ')

N.B. I am surprised that qgis does not support negative look behind though python 2.7 fully supports it.


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:

import re
def spacer(text):
    return re.sub(r'([A-Z])',r" \1",text,re.MULTILINE).strip()

or below if there is a single space already before any capital letter in the passed text.

import re
def spacer(text):
    return re.sub(r'([A-Z])',r" \1",text,re.MULTILINE).strip().replace("  "," ")




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 input text 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 used strip() 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: Here r is just a formatting flag and in space+\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.


In the field calculator:

regexp_replace("FIELD",'\\B([A-Z])',' \\1')

For your example:

regexp_replace('GeorgeJohnMike','\\B([A-Z])',' \\1')


George John Mike

This works in QGIS:

 regexp_replace("field", '(?!^)(?=[A-Z])', ' ')

Short regex explanation:

  • (?=[A-Z]) is a positive lookahead that tells the regex engine to stop before each capital letter
  • (?!^) is a negative lookahead that excludes the beginning of the input string (thus avoiding to take into account the first character if it is a capital letter).
  • This solution adds a space in the begining of the string and also adds an additional space when there are two following words and the second starts with lower case letter. Nov 14, 2016 at 11:11
  • @GISFORESTRY: I've corrected my answer in response to the first part of your comment (leading space), but I'm not sure to understand the second part ("...adds an additional space when there are two following words and the second starts with lower case letter"). Could you please provide an example of what you're meaning?
    – ArMoraer
    Nov 14, 2016 at 13:16

Not completely a GIS question, and you'll find better code that mine in SO, but here is a Python code that you could use in arcGIS field calculator.

def addSpace(word):
    for letter in word[1:]:
        if letter.isupper():
            outword+=' ' + letter
            outword+= letter
    return outword

And if you like one liners:

(''.join([" " + letter if letter.isupper() else letter for letter in word])).lstrip()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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