import re
text = "[document 23] 23 and [document 34] 48 are white [document 24] 240 text"
print( re.sub(r'\[document\s+(\d+)]\s+\1(?!\d)', r'\1', text) )
aW1wb3J0IHJlCnRleHQgPSAiW2RvY3VtZW50IDIzXSAyMyBhbmQgW2RvY3VtZW50IDM0XSA0OCBhcmUgd2hpdGUgW2RvY3VtZW50IDI0XSAyNDAgdGV4dCIKcHJpbnQoIHJlLnN1YihyJ1xbZG9jdW1lbnRccysoXGQrKV1ccytcMSg/IVxkKScsIHInXDEnLCB0ZXh0KSApCg==