Getting started with Regular Expressions
<p>I'm trying to simply play with regular expressions in the console, but I can't.</p>

<p>What am I doing wrong? I'm on python 3.5 I think.</p>

<p>First I tried to use <code>.replace</code> on a string object. </p>

<p>then I imported the <code>re</code> module, but even that didn't work with <code>re.sub</code></p>

<p>I'm basically at a loss. I just want to experiment with regex so I can learn how to use them. </p>

<p>Can you help me get started?</p>

<p>The code that hasn't worked for me is this:</p>

<pre><code>m = "555.555.5555"
mm = m.str.replace(r"'.'","helloworld")
mm
&gt;&gt; 555.555.5555
import re
mm = re.sub(r"'.'","helloworld",m)
mm
&gt;&gt; 555.555.5555
</code></pre>

<p>p.s. </p>

<p>I have some code here that I was trying to emulate</p>

<pre><code>df.CODE.str.replace(r"\A'(E?[V\d]\d\d)(\d*).*", r'\1.\2')
</code></pre>

<p>so that actually works, but I don't understand why. everything I read about says replace doesn't use regex, that you're supposed to use the <code>re</code> module but anyway. I don't understand why it works.</p>

<p>The problem is that you are trying to use regular expressions in string replaces - string replace does <strong>not</strong> support regular expressions for that you need to import and use the re or regex library.</p>

<pre><code>&gt;&gt;&gt; import re
&gt;&gt;&gt; m = "555.555.5555"
&gt;&gt;&gt; &gt;&gt;&gt; mm = re.sub(r"(\d\d)\.(\d)?", m, "helloworld")
&gt;&gt;&gt; mm
'helloworld'
</code></pre>

python regex python-3.x console