fork download
  1. import re
  2. text = "[document 23] 23 and [document 34] 48 are white [document 24] 240 text"
  3. print( re.sub(r'\[document\s+(\d+)]\s+\1(?!\d)', r'\1', text) )
  4.  
Success #stdin #stdout 0.02s 9612KB
stdin
Standard input is empty
stdout
23 and [document 34] 48 are white [document 24] 240 text