我正在使用 Obsidian 和 Obsidian_to_Anki-插件。我需要正则表达式将页面的所有一级标题与捕获组匹配,但仅当页面以
#match
开头时。该插件使用多行标志编译正则表达式。页面具有以下结构:
#match
# Heading 1
Text of Heading 1
# Heading 2
Text of Heading 2
# Heading 3
Text of Heading 3
这不应该匹配:
# Heading 1
Text of Heading 1
# Heading 2
Text of Heading 2
# Heading 3
Text of Heading 3
我想出了这个正则表达式:
#match\s\s(# .*)
。但这样只有 Heading 1
与捕获组 1 匹配,因为 #match
之前没有 Heading 2
。
有办法解决这个问题吗?
提前致谢!
(?:\A#match|\G(?!\A)) # Match '#match' at the start of the file, or the end of the last match
\s* # followed by 0 or more whitespaces;
\K # we forfeit everything we just matched
^(# .+)\n # then match and capture a heading, before continuing to the next line
([\s\S]+?) # and capture the section's content,
(?=\n# |\Z) # which must precedes either another heading or the end of file.
在 regex101.com 上尝试一下。
这利用了以下元序列:
\A
:整个字符串的开头
\G
:最后一场比赛结束或
\A
\G(?!\A)
:仅最后一场比赛结束
\K
:放弃与其左侧表达式匹配的所有内容
\Z
:字符串的结尾或最后一行终止符之前的位置iff它也是整个字符串的最后一个字符。