Regex: AttributeError: 'NoneType' object has no attribute 'groups'
import re
htmlString = '</dd><dt> Fine, thank you. </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.
Python regex AttributeError: 'NoneType' object has no attribute 'group'
I managed to figure out this solution: omit group()
for the situation where the searchbox reply is "No results"
and thus doesn't match the Regex.
try:
searchbox_result = re.match("^.*(?=(\())", searchbox).group()
except AttributeError:
searchbox_result = re.match("^.*(?=(\())", searchbox)
AttributeError: 'NoneType' object has no attribute 'group' ,
The method re.match(..)
returns Match
object (which has .group(x)
methods and etc) or None
in case if the match was not found. In your case the error means that was returned None
;)
Ok, it means that regex pattern doesn't work for the data tested. I've debugged your both cases and I've been noticed that in the first script you apply the pattern to is "flash:c2900-
but in the second example you are testing the regex against file is \"flash:/c2900
where between flash:
and c2900
we have an extra /
which doesn't exist in the first example.
Ok, so there are 2 ways to fix it - if you want to work it with and without /
using the same regex, it will be this way
import re
output = 'System image file is "flash:c2900-universalk9-mz.SPA.156-3.M6.bin"'
print(re.search(r'"flash:/?(.*)"', output).group(1))
output = 'System image file is "flash:/c2900-universalk9-mz.SPA.156-3.M6.bin"'
print(re.search(r'"flash:/?(.*)"', output).group(1))
using optional regex matching (?
).
If you want to work only with /
or without you can use these examples.
import re
output = 'System image file is "flash:c2900-universalk9-mz.SPA.156-3.M6.bin"'
print(re.search(r'"flash:(.*)"', output).group(1))
output = 'System image file is "flash:/c2900-universalk9-mz.SPA.156-3.M6.bin"'
print(re.search(r'"flash:/(.*)"', output).group(1))
AttributeError: 'NoneType' object has no attribute 'group'
'NoneType' object has no attribute 'group' appears when your regex has not matched anything inside the provided input string, because the match object is None, not initialized, the .group()
is None.
The \d\d-\d\d
pattern will only match 28-31
in 28-31 34TH ST
, -\s\d\d
will only match - 21
in 217- 219 EASTERN PARKWAY
, and \d\s-\d
will match 1 -3
and 9 -2
in 1 -3 JANE STREET
and 219 -223 78TH STREET
.
To match the initial digits with a hyphen, you can use
^\d+\s*-\s*\d+
See the regex demo
In code, you can use re.match
to match in the beginning of a string rather than use ^
with re.search
:
if row['address']: # Check if address is present
add = re.match(r'\d+\s*-\s*\d+', row['address']) # Run the regex
if add: # Regex matched?
print(add.group()) # Print the match value
Regular expressions in Python - AttributeError: 'NoneType' object has no attribute 'group'
your search is returning None. Try this instead :
path = '/my_path'
for file in os.listdir(path):
if file.startswith("first"):
print file + '\n'
Related Topics
How to Install Pip for Python 2.6
Simple Animation Using Tkinter
Understanding .Get() Method in Python
Return Result from Python to Vba
Detect Text Region in Image Using Opencv
Distributing My Python Scripts as Jar Files with Jython
How to Change Spacing Between Ticks in Matplotlib
Selenium Webdriver: How to Download a PDF File with Python
Random Row Selection in Pandas Dataframe
How to Fix "Webdriverexception: Message: Connection Refused"
In Python, How to Escape Newline Characters When Printing a String