假设以下字符串
s = '\nThe model layer\nDjango provides an abstraction layer (the “models”) for structuring and manipulating the data of your Web application. Learn more about it below:\n\nModels: Introduction to models | Field types | Indexes | Meta options | Model class\nQuerySets: Making queries | QuerySet method reference | Lookup expressions\nModel instances: Instance methods | Accessing related objects\nMigrations: Introduction to Migrations | Operations reference | SchemaEditor | Writing migrations\nAdvanced: Managers | Raw SQL | Transactions | Aggregation | Search | Custom fields | Multiple databases | Custom lookups | Query Expressions | Conditional Expressions | Database Functions\nOther: Supported databases | Legacy databases | Providing initial data | Optimize database access | PostgreSQL specific features\n'
按findall
检索标题:
In [17]: chapter = re.findall(r'\n.+:', s)
In [18]: chapter
Out[18]:
['\nDjango provides an abstraction layer (the “models”) for structuring and manipulating the data of your Web application. Learn more about it below:',
'\nModels:',
'\nQuerySets:',
'\nModel instances:',
'\nMigrations:',
'\nAdvanced:',
'\nOther:']
效果很好,但finditer
In [19]: chap = re.finiter(r'\n.+:', s)
In [21]: [ i for i in chap]
Out[21]:
[<_sre.SRE_Match object; span=(16, 162), match='\nDjango provides an abstraction layer (the “mode>,
<_sre.SRE_Match object; span=(163, 171), match='\nModels:'>,
<_sre.SRE_Match object; span=(247, 258), match='\nQuerySets:'>,
<_sre.SRE_Match object; span=(322, 339), match='\nModel instances:'>,
<_sre.SRE_Match object; span=(384, 396), match='\nMigrations:'>,
<_sre.SRE_Match object; span=(482, 492), match='\nAdvanced:'>,
<_sre.SRE_Match object; span=(670, 677), match='\nOther:'>]
比较他们的索引[0]
'\nDjango provides an abstraction layer (the “models”) for structuring and manipulating
the data of your Web application. Learn more about it below:',
[<_sre.SRE_Match object; span=(16, 162),
match='\nDjango provides an abstraction layer (the “mode>,
finditer
未能删除已完成的段落。
参考官方文档,对匹配长度没有限制。6.2. re — Regular expression operations — Python 3.6.4rc1 documentation
问题是什么?
答案 0 :(得分:0)
如果您注意到,第一个项目跨越索引16到162,长度为146个字符。 finditer
有效,但其表示(即其__repr__()
方法的输出)正如此处所示,正如kindall的评论中所述。