title: RegEx to insert spaces between (Number-Number), (Number-char), (Char-Special char) and (special char- char) except '\'
body: <p>I have written a code to clean the <code>LaTex</code> (Just a string) where I want to insert spaces among the nnumbers to tokenize the string. My code is as below:</p>
  <pre><code>def insert_spaces(sentence):
    '''
    Add a space around special characters, number and digits. So &quot;2x+y -1/3x&quot; becomes: &quot;2 x + y - 1 / 3 x&quot;
    '''
    dummy_list = []
    splitted_sent = list(sentence)
      
    for i in range(len(splitted_sent)-1):
      dummy_list.append(splitted_sent[i])
      
      if splitted_sent[i].isalpha(): # if it is an alphabet
        if splitted_sent[i+1].isdigit() or (not splitted_sent[i+1].isalnum()):
          dummy_list.append(' ')
      
      elif splitted_sent[i].isdigit(): # if it is a number
        if splitted_sent[i+1].isalpha() or (not splitted_sent[i+1].isalnum()):
          dummy_list.append(' ')
          
      elif (not splitted_sent[i].isalnum()) and (splitted_sent[i] not in [' ','\\']): # if it is a special char but not ' ' already
        if splitted_sent[i+1].isalnum():
          dummy_list.append(' ')
          
    dummy_list.append(splitted_sent[-1])
    
    return ''.join(dummy_list)
  </code></pre>
  <p>For Example, if my original query is:</p>
  <pre><code>'ds^{2} = (1 - {qcos\\theta\\over r})^{2\\over 1 + \\alpha^{2}}\\lbrace dr^2+r^2d\\theta^2+r^2sin^2\\theta d\\varphi^2\\rbrace -{dt^2\\over  (1 - {qcos\\theta\\over r})^{2\\over 1 + \\alpha^{2}}}\\, .\\label{eq:sps1} \\widetilde\\gamma_{\\rm hopf}\\simeq\\sum_{n&gt;0}\\widetilde{G}_n{(-a)^n\\over2^{2n-1}}\\label{H4}3455'
  </code></pre>
  <p>then I want it cleaned as:</p>
  <pre><code>'d s ^ { 2 } = ( 1  - { q c o s \\theta \\over  r } ) ^ { 2 \\over  1  + \\alpha ^ { 2 } } \\lbrace  d r ^ 2 + r ^ 2 d \\theta ^ 2 + r ^ 2 sin ^ 2 \\theta  d \\varphi ^ 2 \\rbrace  -{ d t ^ 2 \\over   ( 1  - { q c o s \\theta \\over  r } ) ^ { 2 \\over  1  + \\alpha ^ { 2 } } } \\ , . \\label { eq : sps 1 } \\widetilde \\gamma _ { \\rm  h o p f } \\simeq \\sum _ { n &gt; 0 } \\widetilde { G } _ n { ( - a ) ^ n \\over 2 ^ { 2 n - 1 } } \\label { H 4 } 3 4 5 5'
  </code></pre>
  <p><a href="https://github.com/ritheshkumar95/im2latex-tensorflow/blob/master/im2markup/scripts/preprocessing/preprocess_formulas.py" rel="nofollow noreferrer">The above result is a product of this this script</a> which basically calls <a href="https://github.com/ritheshkumar95/im2latex-tensorflow/blob/master/im2markup/scripts/preprocessing/preprocess_latex.js" rel="nofollow noreferrer">this <code>KaTex</code> script</a></p>
  <p>But right now, I am getting my results as:</p>
  <pre><code>'ds ^{ 2 } = ( 1  - { qcos \\theta \\over  r })^{ 2 \\over  1  + \\alpha ^{ 2 }}\\lbrace  dr ^ 2 + r ^ 2 d \\theta ^ 2 + r ^ 2 sin ^ 2 \\theta  d \\varphi ^ 2 \\rbrace  -{ dt ^ 2 \\over   ( 1  - { qcos \\theta \\over  r })^{ 2 \\over  1  + \\alpha ^{ 2 }}}\\, .\\label { eq : sps 1 } \\widetilde \\gamma _{\\rm  hopf }\\simeq \\sum _{ n &gt; 0 }\\widetilde { G }_ n {(- a )^ n \\over 2 ^{ 2 n - 1 }}\\label { H 4 } 3455'
  </code></pre>
  <p>Is there any way of achieving the same with the help of RegEx?</p>