Python Regular Expression [\d+]
<p>I am working on regular expression python, I came across this problem.</p>

<p>A valid mobile number is a ten digit number starting with a 7,8 or 9.
my solution to this was :</p>

<pre><code>if len(x)==10 and re.search(r'^[7|8|9]+[\d+]$',x):
</code></pre>

<p>for which i was getting error. later I changed it to</p>

<pre><code>if len(x)==10 and re.search(r'^[7|8|9]+\d+$',x):
</code></pre>

<p>for which all test cases passed. I want to know what the difference between using and not using <code>[]</code> for <code>\d+</code> in regex ?</p>

<p>Thanks  </p>

<p>I think a general explanation about <code>[]</code> and <code>+</code> is what you need.  </p>

<p><code>[]</code> will match with a single character specified inside.<br>
Eg: <code>[qwe]</code> will match with <code>q</code>, <code>w</code> or <code>e</code>.  </p>

<p><strong>If you want to enter an expression inside <code>[]</code>, you need to use it as <code>[^ expression]</code>.</strong></p>

<p><code>+</code> will match the preceding element one or more times.
Eg: <code>qw+e</code> matches <code>qwe</code>, <code>qwwe</code>, <code>qwwwwe</code>, etc...<br>
Note: this is different from <code>*</code> as <code>*</code> matches preceding element zero or more times. i.e. <code>qw*e</code> matches <code>qe</code> as well.</p>

<p><code>\d</code> matches with numerals. (not just <code>0-9</code>, but numerals from other language scripts as well.)</p>

python regex