How can I replace all occurrences of a substring using regex?
<p>I have a string, <code>s = 'sdfjoiweng%@$foo$fsoifjoi'</code>, and I would like to replace <code>'foo'</code> with <code>'bar'</code>. </p>

<p>I tried <code>re.sub(r'\bfoo\b', 'bar', s)</code> and <code>re.sub(r'[foo]', 'bar', s)</code>, but it doesn't do anything. What am I doing wrong?</p>

<p>You can use replace function directly instead of using regex.</p>

<pre><code>&gt;&gt;&gt; s = 'sdfjoiweng%@$foo$fsoifjoifoo'
&gt;&gt;&gt;
&gt;&gt;&gt; s.replace("foo","bar")
'sdfjoiweng%@$bar$fsoifjoibar'
&gt;&gt;&gt;
&gt;&gt;&gt;
</code></pre>

python regex