29

I have a string which I want to extract a subset of. This is part of a larger Python script.

This is the string:

import re

htmlString = '</dd><dt> Fine, thank you.&#160;</dt><dd> Molt bé, gràcies. (<i>mohl behh, GRAH-syuhs</i>)'

Which I want to pull-out "Molt bé, gràcies. mohl behh, GRAH-syuhs". And for that I use regular expression using re.search:

SearchStr = '(\<\/dd\>\<dt\>)+ ([\w+\,\.\s]+)([\&\#\d\;]+)(\<\/dt\>\<dd\>)+ ([\w\,\s\w\s\w\?\!\.]+) (\(\<i\>)([\w\s\,\-]+)(\<\/i\>\))'

Result = re.search(SearchStr, htmlString)

print Result.groups()
AttributeError: 'NoneType' object has no attribute 'groups'

Since Result.groups() doesn't work, neither do the extractions I want to make (i.e. Result.group(5) and Result.group(7)). But I don't understand why I get this error? The regular expression works in TextWrangler, why not in Python? Im a beginner in Python.

martineau
  • 119,623
  • 25
  • 170
  • 301
jO.
  • 3,384
  • 7
  • 28
  • 38

2 Answers2

59

You are getting AttributeError because you're calling groups on None, which hasn't any methods.

regex.search returning None means the regex couldn't find anything matching the pattern from supplied string.

when using regex, it is nice to check whether a match has been made:

Result = re.search(SearchStr, htmlString)

if Result:
    print Result.groups()
thkang
  • 11,215
  • 14
  • 67
  • 83
  • Seems to be a problem with escaping the () in (mohl behh, GRAH-syuhs). I have tried both '\(' and '\\(' but neither seem to work. – jO. Mar 06 '13 at 08:52
  • 1
    Wow I didn't realize all I needed was `if Result:`... Why does that statement work? When reading it, it just feels like it's missing the rest of the if statement. – Fumbles Oct 18 '19 at 14:20
  • because it compares the object != None i think. and if that statement is true you enter the if part – Lion Hunter Nov 19 '20 at 10:32
  • 1
    @Fumbles - It's worth reading about ["truthy and falsy" values in Python](https://www.freecodecamp.org/news/truthy-and-falsy-values-in-python/). You can do lots of cool tricks with if statements - like if you want to test whether a list is empty or not, just do `if my_list:`, and it will run only if the list is non-empty. Empty lists, empty strings, the integer 0, or in this case empty regex matches, all are considered "Falsy", so they fail truth tests in conditionals. – Lou Feb 04 '21 at 14:20
14
import re

htmlString = '</dd><dt> Fine, thank you.&#160;</dt><dd> Molt bé, gràcies. (<i>mohl behh, GRAH-syuhs</i>)'

SearchStr = '(\<\/dd\>\<dt\>)+ ([\w+\,\.\s]+)([\&\#\d\;]+)(\<\/dt\>\<dd\>)+ ([\w\,\s\w\s\w\?\!\.]+) (\(\<i\>)([\w\s\,\-]+)(\<\/i\>\))'

Result = re.search(SearchStr.decode('utf-8'), htmlString.decode('utf-8'), re.I | re.U)

print Result.groups()

Works that way. The expression contains non-latin characters, so it usually fails. You've got to decode into Unicode and use re.U (Unicode) flag.

I'm a beginner too and I faced that issue a couple of times myself.

antonavy
  • 480
  • 6
  • 11