Separate the words by a space in a string using python
<pre><code>text = "Avalon:Sydney 04March2009Bondi Street at a conferencemeeting room234". 
</code></pre>

<p>I would like to process the above string in such as way that the words are separated and not joined. </p>

<p>I would like the string to be printed as follows:</p>

<p><code>"Avalon Sydney 04 March 2009 Bondi Street at a conference metting room 234"</code></p>

<p>The string is now separated into individual words. </p>

<p>I would like to use the regular expressions but I failed. </p>

<pre><code>text = "Avalon:Sydney 04March2009Bondi Street at a conferencemeeting room234"
newtext = re.sub('[a-zA-Z0-9_:]','',text)

print text
</code></pre>

<p>the above code does not work. Is there a better regular expression I can use to separate the words. </p>

<p>The regular expression should not be specific to the above sentence only, but a universal regular expression that can observe the above patters and separate into individual words in the same string.</p>

<p>To replace the colon with a space, use <code>.replace</code>:</p>

<pre><code>text = text.replace(':', ' ')
</code></pre>

<p>To split parts of the text alternating between numbers and letters, you can use a <em>positive look behind</em> to catch words preceded by numbers or numbers preceded by words, and then substitute with a back reference with a leading space:</p>

<pre><code>text = re.sub(r'((?&lt;=[a-zA-Z_])\d|(?&lt;=\d)[a-zA-Z_])', r' \1', text)
print text
# 'Avalon Sydney 04 March 2009 Bondi Street at a conferencemeeting room 234'
</code></pre>

<p>The splitting of <code>conferencemeeting</code> into separate words is not something that is up to regex, except of course, you could define a rule on which the splitting should be done.</p>

python