Note that there are some explanatory texts on larger screens.

plurals
  1. POPython pattern matching
    primarykey
    data
    text
    <p>I'm currently in the process of converting an old bash script of mine into a Python script with added functionality. I've been able to do most things, but I'm having a lot of trouble with Python pattern matching.</p> <p>In my previous script, I downloaded a web page and used sed to get the elemented I wanted. The matching was done like so (for one of the values I wanted):</p> <pre><code>PM_NUMBER=`cat um.htm | LANG=sv_SE.iso88591 sed -n 's/.*ol.st.*pm.*count..\([0-9]*\).*/\1/p'` </code></pre> <p>It would match the number wrapped in <code>&lt;span class="count"&gt;&lt;/span&gt;</code> after the phrase "olästa pm". The markup I'm running this against is:</p> <pre><code>&lt;td style="padding-left: 11px;"&gt; &lt;a href="/abuse_list.php"&gt; &lt;img src="/gfx/abuse_unread.png" width="15" height="12" alt="" title="9 anmälningar" /&gt; &lt;/a&gt; &lt;/td&gt; &lt;td align="center"&gt; &lt;a class="page_login_text" href="/pm.php" title="Du har 3 olästa pm."&gt; &lt;span class="count"&gt;3&lt;/span&gt; &lt;/td&gt; &lt;td style="padding-left: 11px;" align="center"&gt; &lt;a class="page_login_text" href="/blogg_latest.php" title="Du har 1 ny bloggkommentar"&gt; &lt;span class="count"&gt;1&lt;/span&gt; &lt;/td&gt; &lt;td style="padding-left: 11px;" align="center"&gt; &lt;a class="page_login_text" href="/user_guestbook.php" title="Min gästbok"&gt; &lt;span class="count"&gt;1&lt;/span&gt; &lt;/td&gt; &lt;td style="padding-left: 11px;" align="center"&gt; &lt;a class="page_login_text" href="/forum.php?view=3" title="Du har 1 ny forumkommentar"&gt; &lt;span class="count"&gt;1&lt;/span&gt; &lt;/td&gt; &lt;td style="padding-left: 11px;" align="center"&gt; &lt;a class="page_login_text" href="/user_images.php?user_id=162005&amp;func=display_new_comments" title="Du har 1 ny albumkommentar"&gt; &lt;span class="count"&gt;1&lt;/span&gt; &lt;/td&gt; &lt;td style="padding-left: 11px;" align="center"&gt; &lt;a class="page_login_text" href="/forum_favorites.php" title="Du har 2 uppdaterade trådar i &amp;quot;bevakade trådar&amp;quot;"&gt; &lt;span class="count"&gt;2&lt;/span&gt; &lt;/td&gt; </code></pre> <p>I'm hesitant to post this, because it seems like I'm asking for a lot, but could someone please help me with a way to parse this in Python? I've been pulling my hair trying to do this, but regular expressions and I just don't match (pardon the pun). I've spent the last couple of hours experimenting and reading the Python manual on regular expressions, but I can't seem to figure it out.</p> <p>Just to make it clear, what I need are 7 different expressions for matching the number within <code>&lt;span class="count"&gt;&lt;/span&gt;</code>. I need to, for example, be able to find the number of unread PMs ("olästa pm").</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload