[Tutor] checking if regex in pattern

Norman Khine norman at khine.net
Sun Nov 11 11:48:22 CET 2012


hello,

i have this code

>>> import re
>>> import BeautifulSoup
>>> matcher = re.compile(r"""<p><label><strong>Origine :<\/strong><\/label>(.*)<\/p>""")
>>> mystring = """ <p> <label class="garanties_adm"> <strong>Les garanties :</strong></label> <img src="/images/adm/icon-garantie-fairtrade-ab.png" /> </p> <label class="garanties_adm"> <strong>Les garanties :</strong></label> <img src="/images/adm/icon-garantie-fairtrade-ab.png" /> <p> <label><strong>Origine :</strong></label> Burkina Faso, Cercle des Sécheurs                         </p> <label><strong>Origine :</strong></label> Burkina Faso, Cercle des Sécheurs                          <p> </p> <p><a href="/product/670/">Ajouter à ma liste d'envies</a></p> """

I can print the text out but I am unsure how to check for the pattern
as I would like to check if pattern exists and then extract the
Country and Producer - in this case: Burkina Faso and  Cercle des
Sécheurs

if i try:

>>> for txt in soup.findAll(text=True):
...     print type(txt)
...
<class 'BeautifulSoup.NavigableString'>
>>> for txt in soup.findAll(text=True):
...     matches = matcher.match(txt)
...     if matches:
...             print txt
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute 'match'
>>>


what am i missing?


More information about the Tutor mailing list