【发布时间】:2021-01-12 13:05:41
【问题描述】:
我正在尝试使用正则表达式解析降价文档,以查找文档中是否有标题(#title)。
我已经设法用这个正则表达式(?m)^#{1}(?!#) (.*) 实现了这一点,问题是我的markdown 中也可以有代码部分,我可以在其中遇到# 标题格式作为注释。
我的想法是尝试找到 # 标题,但如果在行之前有一个 ```language 则不匹配。
这是一个文本示例,我只需要匹配# my title 而不是下面的# helloworld.py,特别是如果缺少# my title(这是我需要找出的):
<!--
.. title: Structuring a Python application
.. medium: yes
.. devto: yes
-->
# my title
In this short article I will explain all the different ways of structuring a Python application, from a quick script to a more complex web application.
## Single python file containing all the code
```python
#!/usr/bin/env python
# helloworld.py
test
【问题讨论】:
-
标题一定要出现吗?如果它存在,它是否总是在第一个短语后面跟着一个
#? -
如果您在代码块之后不需要可能的标题,您可以使用
document.partition("```")[0]而不是完整文档。 -
标题不一定要存在,如果它存在,它也不是第一个短语,但我在下面找到了答案! @追逐