正则的疑惑,总是抓不到!
>>> str='''<td>...
...
...
... 607
...
...
... </td>
... <td>
...
...
...
... 13
...
...
... </td>
... <td>
...
...
...
... 2.14%
...
...
... </td>
... <td>
...
...
...
... 6.78
...
...
... </td>
... <td>
...
...
... 500
...
... </td>
... <td>
...
...
...
... 0.52
...
...
... </td>'''
>>> s2=re.search('<td>(.*?)</td>\s*<td>*?</td>\s*<td>.*?</td>\s*<td>.*?</td>\s*<td>.*?</td>\s*<td>.*?</td>',str)
>>> s2.group(1)
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
>>> s2=re.search('<td>(.*?)</td>\s*<td>*?</td>\s*<td>.*?</td>\s*<td>.*?</td>\s*<td>.*?</td>\s*<td>.*?</td>',str,re.S)
>>> s2
>>> s2=re.search('<td>(.*?)</td>\s*<td>.*?</td>\s*<td>.*?</td>\s*<td>(.*?)</td>\s*<td>>*?</td>\s*<td>(.*?)</td>',str,re.S)
>>> s2
>>> s2=re.search(u'<td>(.*?)</td>\s*<td>.*?</td>\s*<td>.*?</td>\s*<td>(.*?)</td>\s*<td>>*?</td>\s*<td>(.*?)</td>',str,re.S)
>>> s2
>>> s2=re.search('<td>(.*?)</td>\s*<td>.*?</td>\s*<td>.*?</td>\s*<td>.*?</td>\s*<td>>*?</td>\s*<td>.*?</td>',str,re.S)
>>> s2
>>> s2=re.search('<td>(.*?)</td>\s*<td>.*?</td>\s*<td>.*?</td>\s*<td>.*?</td>\s*<td>*?</td>\s*<td>.*?</td>',str,re.S)
>>> s2
>>> s2
这个我明明写正确的,为啥匹配不到请高手指点,谢谢!
回答:
因为你有换行符 .只匹配所有非换行字符 [.\S\s]*
匹配所有
以上是 正则的疑惑,总是抓不到! 的全部内容, 来源链接: utcz.com/a/161608.html