Separate joined words in a string using python
<pre><code>"10JAN2015AirMail standard envelope from HyderabadAddress details:John Cena Palm DriveAdelaide.Also Contained:NilAction Taken:Goods referred to HGI QLD for further action.Attachments:Nil34FEB2004"
</code></pre>

<p>What I want to do is to read this string in python and separate the joined words. What I exactly want is a regular expression to separate the joined words in the string. </p>

<p>I want to read the above string from a file and the output should be as follows:</p>

<pre><code>"10 JAN 2015 AirMail standard envelope from Hyderabad Address details : John Cena Palm Drive Adelaide. Also calculated: Nil Action Taken: Goods referred to USG for further action. Attachments : Nil 60 FEB 2004." 
</code></pre>

<p>(Separate the joined words)</p>

<p>I need to write a regular expression to separate:</p>

<p><code>'10Jan2015AirMail', 'HyderabadAddress', 'details:John', 'DriveAdelaide'</code></p>

<p>Need a regular expression to identify joined words like above and separate them with a space in the same string like </p>

<p>'10 Jan 2015 AirMail, 'Hyderabad Address', 'details : John'</p>

<p>text = open('C:\sample.txt', 'r').read().replace("\n","").replace("\t","").replace("-",""ââ).replace("/"," ") </p>

<p>newtext = re.sub('[a-zA-Z0-9_:]','',text) #This regex does not work.Please assist</p>

<p>print text
print newtext</p>

<p>The above code does not work</p>

<p>I know that this solution can be done pretty much simpler classifying chars in sets (upper, lower, numeric) but I preferred to do a much more verbose solution:</p>

<pre><code>test_text = "10JAN2015AirMail standard envelope from HyderabadAddress details:John Cena Palm DriveAdelaide.Also Contained:NilAction Taken:Goods referred to HGI QLD for further action.Attachments:Nil34FEB2004"
splitted_text = test_text.split(' ')
num = False
low = False
upp = False
result = []

for word in ss:
  new_word = ''
  if not word.isupper() and not word.islower():
    if word[0].isnumeric():
        num = True
        low = False
        upp = False
    elif word[0].islower():
        num = False
        low = True
        upp = False
    elif word[0].isupper():
        num = False
        low = False
        upp = True
    for letter in word:
      if letter.isnumeric():
        if num:
            new_word += letter
        else:
            new_word += ' ' + letter
        low = False
        upp = False
        num = True
      elif letter.islower():
        if low or upp:
            new_word += letter
        else:
            new_word += ' ' + letter
        low = True
        upp = False
        num = False
      elif letter.isupper():
        if low or num:
            new_word += ' ' + letter
        else:
            new_word += letter
        low = False
        upp = True
        num = False
      else:
        new_word += ' ' + letter
    result.append(''.join(new_word))
  else:
    result.append(word)
' '.join(result)
#'10 JAN 2015 Air Mail standard envelope from Hyderabad Address details : John Cena Palm Drive Adelaide . Also Contained : Nil Action Taken : Goods referred to HGI QLD for further action . Attachments : Nil 34 FEB 2004'
</code></pre>

<p>Sometimes one just need to be pointed int he right direction.</p>

python