如何找到标题

2024-04-18 14:15:07 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有正则表达式模式来匹配下面类似reStructuredText的文本中的标题?困难在于等号的数目必须等于标题的长度。在

Some basic text.


=========
One Title
=========

For titles the numbers of sign `=` must be equal to the length of the text title. 


============= 
Another title
============= 

And so on...

Tags: ofthetext文本标题forbasictitle
2条回答

要支持full syntax for section titles,可以使用^{} package

#!/usr/bin/env python3
"""
some text

=====
Title
=====
Subtitle
    

Titles are underlined (or over- and underlined) with a printing
nonalphanumeric 7-bit ASCII character. Recommended choices are "``= -
` : ' " ~ ^ _ * + # < >``".  The underline/overline must be at least
as long as the title text.

A lone top-level (sub)section is lifted up to be the document's (sub)title.
"""
from docutils.core import publish_doctree

def section_title(node):
    """Whether `node` is a section title.

    Note: it DOES NOT include document title!
    """
    try:
        return node.parent.tagname == "section" and node.tagname == "title"
    except AttributeError:
        return None # not a section title

# get document tree
doctree = publish_doctree(__doc__)    
titles = doctree.traverse(condition=section_title)
print("\n".join([t.astext() for t in titles]))

输出:

^{pr2}$

搜索^{cd1>}的匹配项。如果找到匹配,检查第一组、第二组和第三组的长度是否相同。如果是,title是第二组的内容。

相关问题 更多 >