我試圖將字符串拆分爲子字符串,按「AND」術語拆分,然後在 之後清理每個子字符串「garbage」。使用REGEX通過拆分字符串
下面的代碼得到的錯誤:
AttributeError: 'NoneType' object has no attribute 'group'
import re
def fun(self, str):
for subStr in str.split('AND'):
p = re.compile('[^"()]+')
m = p.match(subStr)
print (m.group())
'str'的值是什麼? – Racso
這就是沒有匹配時會發生的情況......在嘗試對它們進行「分組」之前,您必須檢查「m」是否包含任何元素。 – Floris
使用try和except? –