Question

I'm scraping scientific names from a website using regex, and I can't figure out how to not pull the parenthesis with the scientific name.

The HTML is written like this:

<span class="SciName">(Acanthastrea bowerbanki)</span>

My regex is written like this:

regex = '<span class="SciName">(.+?)</span>'

My results look like this:

(Acanthastrea bowerbanki)

But I need them to look like this:

Acanthastrea bowerbanki
Was it helpful?

Solution

You need an extra pair of parentheses, which you must escape with backslashes to make them literal characters:

regex = r'<span class="SciName">\((.+?)\)</span>'

You will use this as in:

import re

text = '<span class="SciName">(Acanthastrea bowerbanki)</span>'
regex = r'<span class="SciName">\((.+?)\)</span>'
m = re.match(regex, text)
print m.group(1)

OTHER TIPS

You don't need to use regex for this.

s = 'blah blah blah (Acanthastrea bowerbanki) blah blah blah'

scientistName = s[s.find("(")+1:s.find(")")]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top