How to modify text that matches a particular regular expression in Python?
<p>I need to mark negative contexts in a sentence. The algorithm goes as follows:</p>

<ol>
<li>Detect a negator (not/never/ain't/don't/ etc)</li>
<li>Detect a clause ending punctuation (.;:!?)</li>
<li>Add _NEG to all the words in between this.</li>
</ol>

<p>Now, I have defined a regex to pick out all such occurences:</p>

<pre><code>def replacenegation(text):
    match=re.search(r"((\b(never|no|nothing|nowhere|noone|none|not|havent|hasnt|hadnt|cant|couldnt|shouldnt|wont|wouldnt|dont|doesnt|didnt|isnt|arent|aint)\b)|\b\w+n't\b)((?![.:;!?]).)*[.:;!?\b]", text)
    if match:
        s=match.group()
        print s
        news=""
        wlist=re.split(r"[.:;!? ]" , s)
        wlist=wlist[1:]
        print wlist
        for w in wlist:
            if w:
                news=news+" "+w+"_NEG"
        print news
</code></pre>

<p>I can detect and replace the matched group. However, I don't know how to recreate the complete sentence after this operation. Also for multiple matches, match.groups() gives me wrong output.</p>

<p>For example, if my input sentence is:</p>

<pre><code>I don't like you at all; I should not let you know my happiest secret.
</code></pre>

<p>Output should be:</p>

<pre><code>I don't like_NEG you_NEG at_NEG all_NEG ; I should not let_NEG you_NEG know_NEG my_NEG happiest_NEG secret_NEG .
</code></pre>

<p>How do I do this?</p>