0

I'm working in Python for the first time and I've used Mechanize to search a website along with BeautifulSoup to select a particular div, now I'm trying to grab a specific sentence with a regular expression. This is the soup object's contents;

    <div id="results">
   <table cellspacing="0" width="100%">
     <tr>
       <th align="left" valign="middle" width="32%">Physician Name, (CPSO#)</th>
       <th align="left" valign="middle" width="36%">Primary Practice Location</th>
       <!-- <th width="16%" align="center" valign="middle">Accepting New Patients?</th> --> 
       <th align="center" valign="middle" width="32%">Disciplinary Info  &amp; Restrictions</th>
     </tr>

    <tr>
        <td>
            <a class="doctor" href="details.aspx?view=1&amp;id= 85956">Hull, Christopher Merritt </a> (#85956)
        </td>
        <td>Four Counties Medical Clinic<br/>1824 Concessions Dr<br/>Newbury ON  N0L 1Z0<br/>Phone: (519) 693-0350<br/>Fax: (519) 693-0083</td>
        <!-- <td></td> --> 
        <td align="center"></td>
    </tr>
  </table>
</div>

(Thank you for the assistance with formatting)

My regular expression to get the text "Hull, Christopher Merritt" is;

patFinderName = re.compile('<a class="doctor" href="details.aspx?view=1&amp;id= 85956">(.*) </a>')

It keeps returning empty and I can't figure out why, anybody have any ideas?

Thank you for the answers, I've changed it to;

patFinderName = re.compile('<a class="doctor" href=".*">(.*) </a>')

Now it works beautifully.

2

2 Answers 2

3

? is a magic token in regular expressions, meaning zero or one of the previous atom. As you want a literal question mark symbol, you need to escape it.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, I had no idea. Thank you, I'm new to regular expressions and something like that hadn't even crossed my mind.
0

You should escape the ? in your regex:

In [8]: re.findall('<a class="doctor" href="details.aspx\?view=1&amp;id= 85956">(.*)</a>', text)
Out[8]: ['Hull, Christopher Merritt ']

2 Comments

Both answers were great but he responded first, sorry. Though thank you for the formatting help.
@user1094705 Yes, I was editing your post while others answering your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.