How to search for a pattern which is variable length 8 alphanumeric in python 2.7 from content type HTML/Text (in Gmail)
<p>I'm new to python. I'm trying to find a pattern from the Inbox of gmail. Able to fetch the gmail content in html format and not as plain text. Also, I'm not able to identify the pattern of the temporary password (which I need to fetch). the password is of length 8 and is randomly picked from @#$-_!0-9a-zA-Z The password is in the span tag. Here is the part of content fetched from gmail which is in the variable 'body':</p>

<pre><code>Helvetica;font-size: 14px;font-weight: normal;text-align: ce=
nter;"&gt; &lt;span style=3D"font-size:28px"&gt;orPYG$XV&lt;/span&gt;&lt;!----&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/=
tbody&gt; &lt;/table&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;!--[if gte mso 9]&gt;&lt;/td&gt;
</code></pre>

<p>The part of my python code to fetch :</p>

<pre><code>passwordd =re.findall(r'&lt;span style=3D"font-size:28px"&gt;+.*&lt;/span&gt;', str(body), re.I|re.M)
lookkk = re.findall(r'(?&lt;![A-Za-z0-9]))', str(passwordd))
print(str(lookkk))
</code></pre>

<p>where:<br>
body: is the email content I fetched from the gmail inbox in HTML form<br>
passwordd: is a variable created to extract the content from the email text<br>
lookkk: is the final out I'm looking for that is the length 8 password</p>

<p>The passwordd is able to fetch the password including the <code>&lt;span...span&gt;</code>. I want to exclude the <code>&lt;span...span&gt;</code>. How can I do that? Also, is it possible to get the plain text from gmail instead of text in html form. I looked at many forums but couldn't do that.</p>

<p>You need a capturing group inside your regex, they are declared with parenthesis :</p>

<pre><code>pswrd = re.findall(r'&lt;span style=3D"font-size:28px"&gt;+(.*)&lt;/span&gt;', str(body), re.I|re.M)
</code></pre>

<p>To make this more accurate, instead of capturing everything with <code>.*</code> you can also make a more specific search matching exactly what the word is expected to be : <code>[@#$_!0-9a-zA-Z]{8}</code>, so only one expression is enough to find the word.</p>

<p>try <a href="https://regex101.com/r/oR7vX8/1" rel="nofollow">your example on regex101</a> </p>

python html