Using a function as argument to re.sub in Python?
<p>I'm writing a program to split the words contained in an hashtag.</p>

<p>For example I want to split the hashtags:</p>

<pre class="lang-none prettyprint-override"><code>#Whatthehello #goback
</code></pre>

<p>into:</p>

<pre class="lang-none prettyprint-override"><code>What the hello go back
</code></pre>

<p>I'm having troubles when using <a href="https://docs.python.org/3.5/library/re.html#re.sub" rel="nofollow noreferrer"><code>re.sub</code></a> with a functional argument.</p>

<p>The code I've written is:</p>

<pre><code>import re,pdb

def func_replace(each_func):
    i=0
    wordsineach_func=[] 
    while len(each_func) &gt;0:
        i=i+1
        word_found=longest_word(each_func)
        if len(word_found)&gt;0:
            wordsineach_func.append(word_found)
            each_func=each_func.replace(word_found,"")
    return ' '.join(wordsineach_func)

def longest_word(phrase):
    phrase_length=len(phrase)
    words_found=[];index=0
    outerstring=""
    while index &lt; phrase_length:
        outerstring=outerstring+phrase[index]
        index=index+1
        if outerstring in words or outerstring.lower() in words:
            words_found.append(outerstring)
    if len(words_found) ==0:
        words_found.append(phrase)
    return max(words_found, key=len)        

words=[]
# The file corncob_lowercase.txt contains a list of dictionary words
with open('corncob_lowercase.txt') as f:
    read_words=f.readlines()

for read_word in read_words:
    words.append(read_word.replace("\n","").replace("\r",""))
</code></pre>

<p>For example when using these functions like this:</p>

<pre><code>s="#Whatthehello #goback"

#checking if the function is able to segment words
hashtags=re.findall(r"#(\w+)", s)
print func_replace(hashtags[0])

# using the function for re.sub
print re.sub(r"#(\w+)", lambda m: func_replace(m.group()), s)
</code></pre>

<p>The output I obtain is:</p>

<pre class="lang-none prettyprint-override"><code>What the hello
#Whatthehello #goback
</code></pre>

<p>Which is not the output I had expected:</p>

<pre class="lang-none prettyprint-override"><code>What the hello
What the hello go back
</code></pre>

<p>Why is this happening? In particular I've used the suggestion from <a href="https://stackoverflow.com/a/18737964/3646408">this answer</a> but I don't understand what goes wrong in this code.</p>